Building a Swift framework with references to Objective-C code
Asked Answered
H

1

18

I'm working on an iOS project written in Swift, and I would like to take some classes out of the regular "app" project, and bundle it into a reusable Swift framework instead (and maybe make it publicly available on Github and via Cocoapods).

I'm running into issues because frameworks seemingly can't have Objective-C bridging headers, but in order to compile my framework code, I need to reference several Objective-C classes (in this case: the Google Maps iOS SDK).

I've also added GoogleMaps.framework as a linked library in my framework project, but then, how can I "import" it from Swift code?

Is this even possible with the current tools and Swift version, and how should I proceed?

Thanks.

Horntail answered 2/3, 2015 at 17:5 Comment(0)
H
10

It wasn't that complicated, actually... I was just doing some things wrong.

First, bridging headers are not required in that setting: the Google Maps iOS SDK is provided as a regular .framework file, so the development language has no impact on how it can be imported in Swift. Apple clearly mentions it in the documentation: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html, "Importing external frameworks".

It's as easy as adding the framework to the "Link binary with libraries" section of the project settings. Do not forget to also add depending libraries and frameworks (in GoogleMaps.framework's case, there are quite a few).

Then, in Swift code, the framework classes should be available simply by doing:

import GoogleMaps

No bridging header, no dealing with "non-modular header etc." errors.

Horntail answered 9/3, 2015 at 7:43 Comment(5)
Thanks for your answer, I just encountered the same problem, but when I try to import a library, in my case SVProgressHUD xcode shows the ewrror of No such module 'SVProgressHUD'Leffert
@SiuChungChan, are you also building a framework using Cocoapod? Your problem might not be exactly the same as the one I had. Perhaps it would be better to start a new question and provide more information?Horntail
You weren't using a cocoapod, from the sounds of things, Romain?Infidelity
Hey @GeoffreyWiseman, as far as I remember, no, not at that time. But I do now, even though I'm also facing issues when mixing ObjC, Swift, Cocoapods and custom frameworks. But that's another topic...Horntail
Hey @SiuChungChan did you find the answer for your question I am having the same issue while useing SVProgresesHUD in my swift code.Granitite

© 2022 - 2024 — McMap. All rights reserved.