Import 3rd party framework into Swift Cocoa Touch Framework - ios

I have been looking everywhere and trying everything but I can't get this to work. I'm trying to create a Cocoa Touch Framework (in Swift) to share code between an iOS app I'm developing and its extension. All of the shared code relies on the NMSSH framework. I added the NMSSH framework to my framework, made sure it showed up in "Link Binary with Libraries" and that it was targeted at my framework. However, when I add #import <NMSSH/NMSSH.h> to my umbrella header, I get the error Include of non-modular header inside framework module 'MyFramework'.
In most of the places I have looked, they say the fix to this is to import the 3rd party framework in a .m file in my framework. The problem is that my framework is in Swift and thus doesn't have any .m files. I tried adding import NMSSH to my .swift file, but that just results in No such module 'NMSSH'.
There must be a way to actually get this to work without rewriting all of my code in Objective-C.

Related

How to use Swift framework in Objective-c app

I would like to use a Swift library in my Objective-C IOS app. I have the Swift library setup as a separate project, which builds fine in XCode. I can drag the generated Swift framework from the Swift project into the "Frameworks, Libraries and embedded content" list of my Objective-C project target. It appears with an "Embed & Sign" label.
The Apple description here: https://developer.apple.com/documentation/swift/importing-swift-into-objective-c states that
You can work with types declared in Swift from within the Objective-C
code in your project by importing an Xcode-generated header file. This
file is an Objective-C header that declares the Swift interfaces in
your target, and you can think of it as an umbrella header for your
Swift code. You don’t need to do anything special to create the
generated header—just import it to use its contents in your
Objective-C code.
When I look inside the swift .framework file, I can see the header file there. But when I import it in one of my Objective-c .m files, then the compiler says that the file is not found. I have tried both the
#import "Starscream-Swift.h"
and the
#import <Starscream/Starscream-Swift.h>
syntax.
How can I convince XCode to use the header file from the Swift framework? Do I need to copy that header somewhere, maybe? And what else do I need to think about to use a Swift library from an Objective-c IOS app?
I am using XCode version 13.4.1.
By the way, the Swift library I am trying to use is Starstream.
Xcode-generated header file is required when your own project has Objective-C and Swift classes mixed. When dealing with frameworks you either include the framework's umbrella header like this:
#import "SwiftFramework/SwiftFramework.h"
Or, more preferably, you import it with #import expression:
#import SwiftFramework;
Be advised, that only part that is exposed to Objective-C runtime is accessible from this framework (i.e. public/open classes inherited from NSObject or some other Cocoa classes)

How to use Obj-C Framework code in Swift Framework?

How to use Obj-C Framework code in Swift Framework?
Is it possible or I need to mix them in a single Framework?
I have code for both Frameworks. I connect them manually without CocoaPods, Carthage or Swift Package Manager. Bridging header cannot be used inside of a Framework, only in App Target. Google says that I need to use modulemap file. In all examples I found they create their own modulemap files and put them near the Swift framework code. I tried to use both my own modulemap file and existing one inside ObjCFramework.framework/Modules. I use them by adding a path to the modulemap file to SWIFT_INCLUDE_PATHS build setting of my Swift Framework. I also tried to #import Obj-C Frameworks Ubrella Header to Swift Frameworks Ubrella Header with no luck. Of course I added the Obj-C Framework target to my Swift Framework target dependencies, the framework itself to Frameworks and Libraries and the path to it to Framework Search Paths. I didn't put anything to Header Search Paths to avoid "Include of non-modular header inside framework module", but that didn't help. CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES build setting doesn't help too. DEFINES_MODULE is set to YES.
Now I'm in endless loop of several errors:
Include of non-modular header inside framework module 'ObjCFramework': '/Users/bond/Library/Developer/Xcode/DerivedData/SwiftFramework-aqidpocynynsvdgtrqwukanwqkxf/Build/Products/Debug-iphoneos/ObjCFramework.framework/Headers/View.h'
Module 'ObjCFramework' was built in directory '/Users/bond/Library/Developer/Xcode/DerivedData/SwiftFramework-aqidpocynynsvdgtrqwukanwqkxf/Build/Products/Debug-iphoneos/ObjCFramework.framework' but now resides in directory '/Users/bond/Library/Developer/Xcode/DerivedData/SwiftFramework-aqidpocynynsvdgtrqwukanwqkxf/Build/Products/Debug-iphoneos/ObjCFramework.framework/Modules'
No such module ObjCFramework
underlying Objective-C module not found
In brief this is described here: https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_objective-c_into_swift, "Import Code Within a Framework Target" chapter.
If the Defines Module setting is turned on in your framework target, modern Xcode puts Modules folder in the framework, with Swift versions of all you ObjC public interfaces, making ObjC stuff available either for external Swift code that links your framework or Swift code of the framework itself.
This does not depend on whether your framework target contains any swift code.
Just add you ObjC header to framework target, mark it as Public in "Target membership", include in framework Umbrella header and build.

Adding Objective C framework inside Swift Framework

I am writing a Cocoa Framework for iOS in Swift language. I need to add a third party framework (written in Objective-C) inside this framework. I have added headers to the bridging file. But when i build the project i get the following error:
"using bridging headers with framework targets is unsupported"
You should use import ObjcFrameworkName instead of using bridging header.
To make this possible objc framework must contain .modulemap file with exported module name and umbrella header for all public headers of this framework.

Building a Swift framework with references to Objective-C code

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.
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.

How to use third party lib in embedded dynamic framework for iOS with swift

Now I have a project, like testApp, using some third party lib like alamofire and some others libs in objective-c.
Now I want to add a today widget. According to some tutorial, I created a new target "testAppKit" as a shared dynamic framework, and target "testAppWidget" as today extension. The common code will be in testAppKit for reuse.
Now I need to use third party libs in testAppKit. And added lib and header in build phases of testAppKit. Then I add #import <theLib/TheHeader.h> in testAppKit.h. But there is an error:
Include of non-modular header inside framework module 'testAppKit'
So, I want to know how to use third party libs (maybe in Swift or Objective-C) in this kind of embedded dynamic framework.
I use Dropbox Datastore API in my app and I finally made it working for embedded Cocoa Touch framework to share code for Containing App and Today Extension.
I figured out that in my Swift file in the embedded framework I can import any 3rd party framework I had in the Project (i.e. Farbic.framework, Crashlytics etc.) but not Dropbox.
What was the difference? The "Modules" folder! Dropbox.framework doesn't provide module map file. So I created it based on what I found in Fabric.framework:
Go to the Dropbox.framework folder in your project direcotry.
Create new folder "Modules" and go inside
Create a file called: "module.modulemap"
The content of the file:
framework module Dropbox {
umbrella header "Dropbox.h"
export *
module * { export * }
}
After doing that I needed to add import path.
Go to your Project file
Select your embedded framework target
Go to the "Build Settings" and find "Swift Compiler - Search Paths"
Add path to your Dropbox.framerowk and set "recursive" option.
I wanted to put a screenshot here but I can't do that yet - because of my "reputation" ;)
Now I'm able to do "import Dropbox" in my swift files :)
Hope this can help you :)

Resources