Modular iOS

Distributing universal iOS frameworks as XCFrameworks using Cocoapods

Photo by Susan Holt Simpson on Unsplash

How to distribute XCFrameworks using Cocoapods

1. Download the starter pack

cd $HOME
curl https://github.com/anuragajwani/xcframework-cocoapods-tut/archive/starter.zip -o xcframework-cocoapods-tut.zip -L -s
unzip -q xcframework-cocoapods-tut.zip
rm -rf xcframework-cocoapods-tut.zip
mv xcframework-cocoapods-tut-starter xcframework-cocoapods-tut

2. Build an XCFramework

cd ~/xcframework-cocoapods-tut/MyFramework
sh build_xcframework.sh

3. Create git repo to store and version the XCFramework

cd ~/xcframework-cocoapods-tut
mkdir MyFrameworkDistribution.git
cd MyFrameworkDistribution.git
git init --bare
git clone ~/xcframework-cocoapods-tut/MyFrameworkDistribution.git ~/xcframework-cocoapods-tut/MyFrameworkDistribution
cd ~/xcframework-cocoapods-tut/MyFrameworkDistribution
touch README.md
git add README.md
git commit -m "Initial commit"
git push origin -u master
cp -r ~/xcframework-cocoapods-tut/MyFramework/MyFramework.xcframework ~/xcframework-cocoapods-tut/MyFrameworkDistribution
git add MyFramework.xcframework
git commit -m "Added MyFramework.xcframework"
git tag -a 1.0.0 -m "Version 1.0.0"
git push origin master
git push origin --tags

4. Install Cocoapods

gem install cocoapods
pod --version

5. Create git repo for specification files

cd ~/xcframework-cocoapods-tut/
mkdir MySpecs.git
cd MySpecs.git
git init --bare
git clone ~/xcframework-cocoapods-tut/MySpecs.git ~/xcframework-cocoapods-tut/MySpecs
cd ~/xcframework-cocoapods-tut/MySpecs
touch README.md
git add README.md
git commit -m "Initial commit"
git push origin -u master
pod repo add my-specs ~/xcframework-cocoapods-tut/MySpecs.git

6. Create specification file

cat > ~/xcframework-cocoapods-tut/MyFramework.podspec <<-EOF
Pod::Spec.new do |s|
s.name = "MyFramework"
s.version = "1.0.0"
s.summary = "A brief description of MyFramework project."
s.description = <<-DESC
An extended description of MyFramework project.
DESC
s.homepage = "http://your.homepage/here"
s.license = { :type => 'Copyright', :text => <<-LICENSE
Copyright 2018
Permission is granted to...
LICENSE
}
s.author = { "$(git config user.name)" => "$(git config user.email)" }
s.source = { :git => "$HOME/xcframework-cocoapods-tut/MyFrameworkDistribution.git", :tag => "#{s.version}" }
s.vendored_frameworks = "MyFramework.xcframework"
s.platform = :ios
s.swift_version = "4.2"
s.ios.deployment_target = '12.0'
end
EOF

7. Publish specification file

pod repo push my-specs ~/xcframework-cocoapods-tut/MyFramework.podspec

How to consume XCFrameworks using Cocoapods

1. Create Podfile

cd ~/xcframework-cocoapods-tut/XCFrameworkCocoapodsDemo
cat > Podfile <<-EOF
target 'XCFrameworkCocoapodsDemo' do
use_frameworks!
pod 'MyFramework', '1.0.0', :source => "$HOME/xcframework-cocoapods-tut/MySpecs.git"
end
EOF

2. Install dependencies

pod install

3. Consume XCFramework

open -a Xcode ~/xcframework-cocoapods-tut/XCFrameworkCocoapodsDemo/XCFrameworkCocoapodsDemo.xcworkspace
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

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store