Modular iOS

How to distribute compiled static frameworks via Cocoapods

Photo by Aaron Huber on Unsplash

Why use Cocoapods?

How does Cocoapods work?

How to distribute compiled static frameworks through Cocoapods

1. Download the starter project

cd $HOME
curl https://github.com/anuragajwani/compiled_swift_static_lib/archive/completed.zip -o compiled_swift_static_lib.zip -L -s
unzip -q compiled_swift_static_lib.zip
cd compiled_swift_static_lib-completed/MyStaticLib

2. Build the static framework

sh build_static_framework.sh

3. Store the static framework in a local git repository

cd ~
mkdir MyFrameworkDistribution.git
cd MyFrameworkDistribution.git
git init --bare
git clone ~/MyFrameworkDistribution.git ~/MyFrameworkDistribution
cd ~/MyFrameworkDistribution
touch README.md
git add README.md
git commit -m "Initial commit"
git push origin -u master
cp -r ~/compiled_swift_static_lib-completed/MyStaticLib/MyStaticLib.framework ~/MyFrameworkDistribution
git add MyStaticLib.framework
git commit -m "Added MyStaticLib.framework"
git tag -a 1.0.0 -m "Version 1.0.0"
git push origin master
git push origin --tags

4. Install Cocoapods

sudo gem install cocoapods

5. Create Cocoapods specification repository

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

6. Create specification file

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

7. Publish static framework specification to the specification repository

pod repo push my-specs ~/compiled_swift_static_lib-completed/MyStaticLib/MyStaticLib.podspec

Consuming compiled static frameworks distributed through Cocoapods

1. Create Podfile

cd ~/compiled_swift_static_lib-completed/MyApp
cat > Podfile <<-EOF
target 'MyApp' do
use_frameworks!
pod 'MyStaticLib', '1.0.0', :source => "$HOME/MySpecs.git"
end
EOF

2. Install dependencies

pod install

3. Consume the static framework

open -a Xcode MyApp.xcworkspace
import MyStaticLib
functionA()
console output

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