Skip to content

Latest commit

 

History

History
142 lines (95 loc) · 7.38 KB

README.md

File metadata and controls

142 lines (95 loc) · 7.38 KB

XCFrameworkScripts

Useful Scripts used to build XCFramework and links to learning materials.

Introduction

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.

Archive for iOS iPhone (Release)

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 :

  1. -scheme : It’ll use this scheme for archiving (Make sure you specify a project name/scheme as it appear on the Xcode project).
  2. -configuration Release: It’ll use the release configuration for building.
  3. -destination ‘generic/platform=iOS’: This is the architecture type.
  4. -archivePath: It saves archives into this folder path with the given name.
  5. SKIP_INSTALL: Set NO to install the framework to the archive.
  6. BUILD_LIBRARIES_FOR_DISTRIBUTION: Ensures your libraries are built for distribution and creates the interface file.

Archive for iOS Simulator (Release)

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:

  1. -destination ‘generic/platform=iOS Simulator’: This is where you set the architecture type.
  2. -archivePath: This generates the archive into the folder path with the given name.

Archive for macOS (Release)

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:

  1. -destination ‘platform=macOS,arch=x86_64,variant=Mac Catalyst’: This is where you indicate the architecture type.
  2. -archivePath: This generates the archive into the folder path with the given name.

Generate XCFramework

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'

Learning Materials

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.

Binary Frameworks

  1. Apple Docs: Distributing Binary Frameworks as Swift Packages

  2. Medium: Binary Frameworks in Swift — XCFrameworks

  3. WWDC 2019: Binary frameworks in Swift

  4. WWDC 2020: Distribute Binary frameworks as Swift packages

  5. iOS Deep Understanding of Libraries and Frameworks

Adding SPM to existing frameworks/libraries

  1. Article -> Creating a Swift Package from an existing iOS Framework (Library)

  2. Medium -> Creating XCFramework from Swift Package

  3. Article -> How to create a Swift Package from a CocoaPods project

Swift Package Manager

Credits

  1. Raywenderlich -> Creating a Framework for iOS
  2. Official Apple Docs -> Create an XCFramework
  3. Raywenderlich -> Swift Package Manager for iOS

Related open source projects

The following is the list of open source projects which automate the process or creating frameworks.