I have created one xcframework in Swift Language. I have marked by .swift class with @objc as well.
I tried to use <frameworkname-Swift.h> in objective c project, But it is showing Module not found error.
Can anyone help with how to use xcframework with Objective-C code.
Static libraries (.a) Now just drag and drop your xcframework to "General > Frameworks, Libraries and Embedded Content" with "Do not Embed" because you have static libraries to link. Last step to set "Build Settings > Header Search Path" to "<your_ destination>/SwiftHelloStatic. xcframework/**" to find your headers.
You can use Objective-C and Swift files together in a single project, no matter which language the project used originally. This makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.
An XCFramework can be either static or dynamic and can include headers.
It's very important to make your libraries right before making xcframework and starting integrating them to your project.
Static libraries (.a)
Your class and its methods should be public and marked with @objc
to be visible in Objc:
@objc public class SwiftHelloStatic : NSObject {
@objc static public func hello() {
print("Hello Static Library!")
}
}
First add your "Objective-c Generated Interface Header" (e.g. SwiftHelloStatic-Swift.h) to "Build Phases > Copy Files", for instance from your destination or from DeviredData folder:
Build your needed arch (arm64/x86_64) and then you can find "include" folder in your library's one that contains your header:
Build your xcframework from libraries and headers:
xcodebuild -create-xcframework
-library arm64/libSwiftHelloStatic.a -headers arm64/include
-library x86_64/libSwiftHelloStatic.a -headers x86_64/include
-output SwiftHelloStatic.xcframework
And after verify than your SwiftHelloStatic.xcframework has "Headers" folders inside arch ones:
Now just drag and drop your xcframework to "General > Frameworks, Libraries and Embedded Content" with "Do not Embed" because you have static libraries to link.
Last step to set "Build Settings > Header Search Path" to "<your_ destination>/SwiftHelloStatic.xcframework/**" to find your headers.
And finally you can add headers to your objc file:
#import <SwiftHelloStatic/SwiftHelloStatic-Swift.h>
...
[SwiftHelloStatic hello];
Dynamic framework (.framework)
It's similar to static libraries but simpler because frameworks already have headers. First set Build Library for Distrubution
to YES and then build your framework for needed archs (arm64, x86_64, ...) and build you xcframework from them e.g.:
xcodebuild -create-xcframework
-framework arm64/SwiftHello.framework
-framework x86_64/SwiftHello.framework
-output SwiftHello.xcframework
Now just drag and drop xcframework to "General > Frameworks, Libraries and Embedded Content" with "Embed & Sign".
In your objc project you should import your xcframework next way:
#import <SwiftHello/SwiftHello.h>
Thats because a valid xcframework contains separate dynamic frameworks for all its target platforms and a needed one from them is linked while compilation so that importing headers to your code is the same as for a regular framework.
Also verify that your classes are accessible in "SwiftHello-Swift.h" e.g.:
SWIFT_CLASS("_TtC10SwiftHello5Hello")
@interface Hello : NSObject
+ (void)hello;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With