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