Useful Scripts used to build XCFramework and links to learning materials.
The following scripts can be copy pasted to terminal (Make sure you are inside a project folder) and produce .framework then the final script is used to combine the individual generated archives into single .XCFramework ready for distribution.
Note: Before creating an xcframework builds needs to be archived for all supported architectures as shown on the steps below.
xcodebuild archive \
-scheme <YOUR FRAMEWORK NAME> \
-configuration Release \
-destination 'generic/platform=iOS' \
-archivePath './build/<YOUR FRAMEWORK NAME>.framework-iphoneos.xcarchive' \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
The above command will generate an archive of your framework by using the following list as inputs :
- -scheme : It’ll use this scheme for archiving (Make sure you specify a project name/scheme as it appear on the Xcode project).
- -configuration Release: It’ll use the release configuration for building.
- -destination ‘generic/platform=iOS’: This is the architecture type.
- -archivePath: It saves archives into this folder path with the given name.
- SKIP_INSTALL: Set NO to install the framework to the archive.
- BUILD_LIBRARIES_FOR_DISTRIBUTION: Ensures your libraries are built for distribution and creates the interface file.
xcodebuild archive \
-scheme <YOUR FRAMEWORK NAME> \
-configuration Release \
-destination 'generic/platform=iOS Simulator' \
-archivePath './build/<YOUR FRAMEWORK NAME>.framework-iphonesimulator.xcarchive' \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
These command options are the same as those for iOS except for the following differences:
- -destination ‘generic/platform=iOS Simulator’: This is where you set the architecture type.
- -archivePath: This generates the archive into the folder path with the given name.
xcodebuild archive \
-scheme <YOUR FRAMEWORK NAME> \
-configuration Release \
-destination 'platform=macOS,arch=x86_64,variant=Mac Catalyst' \
-archivePath './build/<YOUR FRAMEWORK NAME>.framework-catalyst.xcarchive' \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
This command is the same as the others except for the following differences:
- -destination ‘platform=macOS,arch=x86_64,variant=Mac Catalyst’: This is where you indicate the architecture type.
- -archivePath: This generates the archive into the folder path with the given name.
This command adds your XCFramework to the build folder using the generated archives above.
xcodebuild -create-xcframework \
-framework './build/<YOUR FRAMEWORK NAME>.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/<YOUR FRAMEWORK NAME>.framework' \
-framework './build/<YOUR FRAMEWORK NAME>.framework-iphoneos.xcarchive/Products/Library/Frameworks/<YOUR FRAMEWORK NAME>.framework' \
-framework './build/<YOUR FRAMEWORK NAME>.framework-catalyst.xcarchive/Products/Library/Frameworks/<YOUR FRAMEWORK NAME>.framework' \
-output './build/<YOUR FRAMEWORK NAME>.xcframework'
As I was working working with XCFrameworks, I found the following online learning materials to be very helpful. These can act as a shortcut to quickly read or watch about frameworks or packages without spending much time to search on the web.
-
Article -> Creating a Swift Package from an existing iOS Framework (Library)
-
Medium -> Creating XCFramework from Swift Package
-
Article -> How to create a Swift Package from a CocoaPods project
-
Swift by Sundell:Managing dependencies using the Swift Package Manager
-
Stackoverflow: Add demo app to SPM
- As a side note, project should be closed before following the steps on the linked answer.
-
Apple Docs: Developing a Swift Package in Tandem with an App
- Raywenderlich -> Creating a Framework for iOS
- Official Apple Docs -> Create an XCFramework
- Raywenderlich -> Swift Package Manager for iOS
The following is the list of open source projects which automate the process or creating frameworks.