How to build a binary framework in Xcode 11
Posted on by Ciprian Redinciuc · 2 min read
Apple announced the introduction of a new way of building and distributing binary frameworks with Xcode 11 at the 2019 WWDC.
This is great news for SDK developers as it allows them to distribute binaries that don't break once a new Swift version is released and it allows them to bundle the code for multiple platforms into one single framework (iOS, iOS Simulator, watchOS, tvOS, etc.).
This is made possible thanks to a new framework format called XCFramework. If you want to find out more about it, check this WWDC session.
Setting up your Framework
If you don't have a framework already, you can create one by adding a new target to your project, as exemplified below.
I won't go into the details of what you should include in the framework and what should be considered public or private, that's fully up to you.
The next step you need to take is to prepare the framework for distribution. To do that, you have to set “Build Libraries for Distribution” to Yes in your framework target build settings as well as setting "Skip Install" to No.
That's it! You're ready to build your first XCFramework.
Building your XCFramework
Now that you have set up your project, let's go on and build it. First, you will need to archive your target. In this example we will archive an iOS Framework for running on a device and in the iOS Simulator:
xcodebuild archive \ -workspace YourProjectWorkspace.xcworkspace \ -scheme YourFramework \ -destination "generic/platform=iOS Simulator" \ -archivePath "archives/YourFramework-Simulator" \ SKIP_INSTALL=NO \ BUILD_LIBRARY_FOR_DISTRIBUTION=YES
To archive the framework for the device we'll have to run a new command:
xcodebuild archive \ -workspace YourProjectWorkspace.xcworkspace \ -scheme YourFramework \ -destination generic/platform=iOS \ -archivePath "archives/YourFramework" \ SKIP_INSTALL=NO \ BUILD_LIBRARY_FOR_DISTRIBUTION=YES
These commands will create two archives in the archives folder: YourFramework and YourFramework-Simulator. We will need to get the compiled frameworks from these archives.
You can do this manually or you can run the following script:
cp -r /archives/YourFramework-Simulator.xcarchive/Products/Library/Frameworks/. /archives/simulator cp -r /archives/YourFramework.xcarchive/Products/Library/Frameworks/. /archives/ios
This will copy the contents of the folder Frameworks from your newly created XCArchives to two new folders inside the archives directory: ios and simulator.
After you've done this, you can run the following command. This will build the XCFramework using the framework files we just copied:
xcodebuild -create-xcframework \ -framework archives/ios/YourFramework.framework \ -framework archives/simulator/YourFramework.framework \ -output archives/YourFramework.xcframework
That's it. You can now drag and drop your XCFramework into other projects or distributed it via Cocoapods by creating a podspec.
Please keep in mind that you cannot include other XCFrameworks into your XCFramework, at least not for iOS. At this point, this is not yet possible in Xcode.