Objective-C Bridging Header for frameworks
Asked Answered
B

2

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.

Brood answered 19/7, 2014 at 13:55 Comment(0)
B
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

Brood answered 19/7, 2014 at 14:21 Comment(8)
This lead me to the error "using bridging headers with framework targets is unsupported". The actual answer is this #24876245.Michelemichelina
Could you elaborate on how to "make the C-header public in the file inspector" ?Trodden
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.Trodden
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.Brood
@GustafRosenblad no problems and thanks for the reply. Since I'm able to use ObjC framework from an ObjC .m file, I'm fine with it for now.Trodden
If set C-header to public, all Objective-C class will be visible outside of the framework. But I don'' want these Objective-C class to be exposed, what should I do?Billposter
@User9527 True. I had the same problem, but I have unfortunately not found a solution.Brood
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 * }Billposter
C
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;
Courcy answered 19/7, 2014 at 14:15 Comment(1)
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 unsupportedValdemar

© 2022 - 2024 — McMap. All rights reserved.