19

I've made a framework that requires the sqlite3 framework. How do I add a Objective-C Bridging Header for my framework that imports sqlite3 into my Swift file?

I already have a bridging header file for my project, but not for my framework.

2 Answers 2

39

I found a Objective-C Bridging Header setting in the target Build Settings. It was hidden by default. Check All instead of Basic.

In recent Xcode versions this solution would give the error Using bridging headers with framework targets is unsupported.

The workaround I've been using is to make the C-header public in the file inspector and import it in MyFramework.h like this example:

#import <MyFramework/MyObjectiveC.h> 

How to change the C-header to public

Open your C-header and view the inspector by clicking in the upper right corner. To view the file inspector, click the file icon in the upper right corner.

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

This lead me to the error "using bridging headers with framework targets is unsupported". The actual answer is this stackoverflow.com/questions/24875745/….
Could you elaborate on how to "make the C-header public in the file inspector" ?
Hi Gustaf, thanks for the quick reply. I'm trying to import an Objective C third party framework into my Swift framework and I get "no such module" error. I'm able to use that framework from objc code but not from swift code in the framework I'm creating. I know this could be a different question.
Sorry @Ravi, but I can't help you there. You can try checking the framework is linked in your target, I don't know anything more that will be to help.
I found a solution for that. @GustafRosenblad , you need create xxx.modulemap, set "Import Path" to import your modulemap. The modulemap file should look like: module OCModule [system] { header "System Services/SystemServices.h" header "networking/Reachability/Reachability.h" export * }
|
3

just import your sqlite3 framework in your objective-c bridging file. You then automatically can use it in Swift.

Apple Docs:

Interoperability is the ability to interface between Swift and Objective-C in either direction, letting you access and use pieces of code written in one language in a file of the other language. As you begin to integrate Swift into your app development workflow, it’s a good idea to understand how you can leverage interoperability to redefine, improve, and enhance the way you write Cocoa apps. One important aspect of interoperability is that it lets you work with Objective-C APIs when writing Swift code. After you import an Objective-C framework, you can instantiate classes from it and interact with them using native Swift syntax.

EDIT: You even can import an Objective-C framework or Swift framework or a mixed-language framework just into your swift file with

import yourFramework

Apple Docs:

Importing External Frameworks

You can import external frameworks that have a pure Objective-C codebase, a pure Swift codebase, or a mixed-language codebase. The process for importing an external framework is the same whether the framework is written in a single language or contains files from both languages. When you import an external framework, make sure the Defines Module build setting for the framework you’re importing is set to Yes.

You can import a framework into any Swift file within a different target using the following syntax:

SWIFT

import FrameworkName 

You can import a framework into any Objective-C .m file within a different target using the following syntax:

@import FrameworkName; 

1 Comment

Using a bridging header is not allowed in Frameworks. Following the above instructions in a framework will produce the above error upon building: error: using bridging headers with framework targets is unsupported

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.