Distributing Swift Frameworks via Cocoapods

????In very few steps we were able to achieve installation of a framework what would take many more steps using only the native Apple provided tools.


Publishing frameworks to CocoapodsNow that we understand how Cocoapods installs frameworks, the only thing we have to do in order for other apps to use our framework is to publish the specification of our framework to the specification repository.

Cocoapods publishLet’s get a feel for it in action!Getting startedIn this guide we will:Install CocoapodsRetrieve an Xcode project containing a framework targetCreate a local git repository to host our framework projectCreate a local specification repositoryPublish our framework specification to our specification repositoryInstall our framework into an appBefore diving in first let’s open the terminal app.

We will be using terminal throughout the guide to accomplish sharing and installation of our framework.


Installing CocoapodsTo start off with let’s install Cocoapods, the tool that will ease sharing and installing frameworks.

In terminal type or, copy & paste the following command.

gem install cocoapodsInstalling Cocoapods2.

Retrieve starter projectNext, let’s setup the starter framework project which we will share via Cocoapods.

Lets download and place the starter project in our home directory via terminal.

cd ~curl -o MyFramework.

zip -L https://www.



zip -sunzip -q MyFramework.


Create locally hosted git repositoryFor this article we’ll be hosting a git repository for the MyFramework project locally in our Macs.

To create a locally hosted git repository execute the following commands:mkdir ~/MyFramework.

gitcd ~/MyFramework.

gitgit init –bareLocally hosting a git repository for MyFramework projectAbove we have created a directory that will hold our git repository (with extension .

git) and initialised a git repository inside that directory.

Now that we have a place where to host our framework lets add the MyFramework project content to that place.

Execute the following commands:cd ~/MyFrameworkgit initgit remote add origin ~/MyFramework.

gitgit add .

git commit -m "Initial commit"git tag -a 0.


1 -m "Version 0.


1"git push origin -u mastergit push origin –tagsStoring MyFramework in git repositoryWe now have our source code stored in a local git repository.


Create local specification repositoryLet’s create a repository to hold specifications.

This is the place where we publish our framework specification.

To do so execute the following commands:mkdir ~/MySpecs.

gitcd ~/MySpecs.

gitgit init –baregit clone ~/MySpecs.

git ~/MySpecs cd ~/MySpecstouch README.

mdgit add README.

mdgit commit -m "Initial commit"git push origin -u masterpod repo add my-specs ~/MySpecs.

gitCreate specification repositoryThe first few commands are similar to the previous section where we created a git repository to hold our framework.

The last command pod repo add my-specs ~/MySpecs.

git tells Cocoapods to add ~/MySpecs.

git to the list of specification repositories and giving it the name of my-specs.

You can check the list of specification repositories held by your Cocoapods by running pod repo list.

List of specification repositories — pod repo list5.

Publish specification for MyFrameworkBefore pushing a Cocoapods specification let’s add a git tag to our project so Cocoapods can refer to specific snapshot of the project.

A git tag will add an identifier to our current state of the project.

As git is not the scope of this guide simply know that addition to our project framework won’t affect the code delivered via Cocoapods as Cocoapods will refer to the current state of the project.

Let’s tag the current state of our framework as version 0.



Run the following commands:cd ~/MyFrameworkgit tag -a 0.


1 -m "Version 0.


1"Next, let’s add a Cocoapods specification to the project.

Run the commands below:cat > ~/MyFramework.

podspec <<-EOFPod::Spec.

new do |s| s.

name = "MyFramework" s.

version = "0.


1" s.

summary = "A brief description of MyFramework project.

" s.

description = <<-DESC An extended description of MyFramework project.


homepage = "http://your.

homepage/here" s.

license = { :type => 'Copyright', :text => <<-LICENSE Copyright 2018 Permission is granted to.


author = { "$(git config user.

name)" => "$(git config user.

email)" } s.

source = { :git => "$HOME/MyFramework.

git", :tag => "#{s.

version}" } s.

source_files = "MyFramework/**/*.

swift" s.

resources = "MyFramework/**/*.

xib" s.

platform = :ios s.

swift_version = "4.

2" s.


deployment_target = '12.

0'endEOFThe above command will create a file called MyFramework.

podspec and add the contents between EOF into the file.

Here we are adding the necessary information for Cocoapods to be able to fetch and install the correct code into the installers project.

You can find other Cocoapods specification attributes to allow you to configure your framework according to your project needs here.

We have now created a specification.

The last thing to do is to make this specification available to the framework consumers by pushing this specification to a specification repository.

Cocoapods publishRun the following command to push MyFramework specification to the private specification repository:pod repo push my-specs ~/MyFramework.

podspecpod repo pushGreat job!.Our framework is now listed in our specification repository and is ready to be consumed by apps!.In the final step we will consume MyFramework via an app.


Install MyFramework in an appBefore we can test the installation of MyFramework via Cocoapods we need an app in which to install it to.

Let’s fetch MyApp project from my previous post.

Run the following commands:cd ~curl -o MyApp.

zip -L https://www.



zip?.-sunzip -q MyApp.

zipNext let’s install MyFramework in MyApp.

We will first have to create a Podfile where we will specify MyFramework to be installed in MyApp.

Run the following commands:cd ~/MyAppcat > Podfile <<-EOFtarget 'MyApp' do use_frameworks!.pod 'MyFramework', '0.


1', :source => "$HOME/MySpecs.

git"endEOFNote use_frameworks!.is required when using frameworks.

Next let’s tell Cocoapods to install the specified frameworks in the Podfile.

Run pod install command to do so.

pod installOpen MyApp.

xcworkspace in Xcode and run the app.

And that all!.????Final notesBefore we end this post, note MyFramework source code is copied into a Pods project and wrapped in a framework target.

Every time we build the app MyFramework is also built and then attached to the app.

Installed MyFramework in MyApp workspaceTargets in Pods projectMyFramework building in MyApp build processSummaryIn this guide we have learnthow cocoapods workhow to distribute a framework through Cocoapodshow to install our distributed framework via CocoapodsWe have seen how simple it is from an integrators point of view to install and manage frameworks using Cocoapods.

Cocoapods can attract more developers to install and consume your framework thanks to its simplicity.

Next stepsIn this guide we saw that Cocoapods shares source code as well as assets with the integrator and then packages them in a framework on the integrators end.

We saw the integrator then compiles that code as part of their app build process.

But what if we don’t want to share the code?.Or maybe we would like to save time from compiling large chunks of code from our apps that are shared and that rarely changes.

Is there a way to shared compiled frameworks via Cocoapods?You will find the answer to that question in the next article.

Follow me on Twitter or Medium and stay tuned!.

. More details

Leave a Reply