Modular iOS

How to build universal iOS frameworks using XCFrameworks

Vector graphics sourced from https://www.vecteezy.com

What is XCFramework?

Why use XCFramework?

How to build an XCFramework

1. Retrieve the starter project

cd $HOME
curl https://github.com/anuragajwani/universal-ios-framework-xcframework/archive/starter.zip -o ios_framework.zip -L -s
unzip -q ios_framework.zip
cd universal-ios-framework-xcframework-starter/MyFramework

2. Build an XCFramework

xcodebuild archive \
-scheme MyFramework \
-sdk iphoneos \
-archivePath "archives/ios_devices.xcarchive" \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
SKIP_INSTALL=NO
xcodebuild archive \
-scheme MyFramework \
-sdk iphonesimulator \
-archivePath "archives/ios_simulators.xcarchive" \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
SKIP_INSTALL=NO
xcodebuild -create-xcframework \
-framework archives/ios_devices.xcarchive/Products/Library/Frameworks/MyFramework.framework \
-framework archives/ios_simulators.xcarchive/Products/Library/Frameworks/MyFramework.framework \
-output build/MyFramework.xcframework

3. Consuming the XCFramework

open ~/universal-ios-framework-xcframework-starter/XCFrameworkDemo/XCFrameworkDemo.xcodeproj
open -a Finder ~/universal-ios-framework-xcframework-starter/MyFramework/build
import MyFramework
let loginViewController = LoginViewController()
self.present(loginViewController, animated: true, completion: nil)

Summary

Final Notes

Senior iOS Engineer @ Onfido. Writing weekly blogs on iOS and programming. Follow me to stay tuned!

Get the Medium app