Distributing iOS Static Library via CocoaPods

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

I was new to cocoa pods and looked at their documentation on how to create a pod and distribute. They mainly focused on examples that involved distributing source files rather than a pre-built iOS static library. Hence I want to make a short post on how the pod spec looks like if you are shipping an iOS static library via Cocoapods.

This post does not list steps on creating pod spec and uploading it to the master repo. I will rather provide links which helped me doing that. Please read through if you already know what is cocoa pods and you have an iOS static library which you need to distribute through Cocoa pods.

If you ended up this page as a newbie to cocoa pods, don’t worry.  Refer the following links and come back.

For information on how to consume cocoa pods as well as how to create one, I found this blog very helpful – http://nshipster.com/cocoapods/

I strongly recommend to test your pods locally before pushing things to master cocoa pods repo: here is how you do that http://www.cocoanetics.com/2013/05/8130/

Assuming you are all setup with your pod specs, below are the attributes of pod spec which is vital in distributing iOS Static library

s.source_files : Specify which files need to be copied. In our case, we need to copy the the Calculator.h header file. The value of the source_file attribute is the relative path of the location of header file from the Podspec location.

s.public_header_files = Specifies the header files to include and search for. Value is the relative path of the header files and the path will be added to User Header file search path in the Xcode build settings.

s.library = the Libraries to link with in order for the static library to compile successfully. In our example, the library requires the stdc++ library. The value is the library name without appending lib as prefix. For e.g., stdc++. If you have multiple libraries as dependency, provide them as a comma separated values.

s.framework = List of frameworks that the library is dependent upon. As every project by default is linked with UIKit, Foundation, You can specify any other frameworks as a comma separated values.

s.preserve_paths = We need to specify the files to be copied during the installation. In our example we need to explicitly copy our libCalculator.a file after installation onto the clients system. The value of this attribute would be the relative path ( from the PodSpec)  of the files to be copied.

s.requires_arc = The value is either true or false. Since our example project supports ARC, we set it as true

s.ios.vendored_library = Specify the library, libCalculator.a to be copied while installing the pod

Additional attributes that might come in handy

s.dependency = Reference to another pods, where the library is dependent upon. For the simplicity of the example, I have not included any libraries as dependents.

s.xcconfig = There would be other build settings, for which there are no corresponding  attributes in Podspec. In such cases, you can use xcconfig to specify the build settings directly.

For example, to set the HEADER_SEARCH_PATH build settings you can specify them in a json style like

{ “HEADER_SEARCH_PATH” => “$(SDKROOT)/usr/include/libxml2“}

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Here is the final PodSpec

Pod::Spec.new do |s|

s.name = “CalcLib”
s.version = “1.0”
s.summary = “Example iOS Static Library – Calculator. ”

s.description = <<-DESC
This iOS Static library is used to explain how to distribute iOS Static library via cocoapods

s.homepage = “https://github.com/bharath2020/CalcLibrary/blob/master/README.md”
s.license = { :type => “BSD”, :file => “LICENSE” }

s.author = { “Bharath Booshan” => “bharath2020@gmail.com” }
s.social_media_url = “http://twitter.com/bharath2020”

s.platform = :ios, “6.0”
s.source = { :git => “https://github.com/bharath2020/CalcLibrary.git”, :tag => s.version.to_s}
s.source_files = “include/*.h”
s.public_header_files = “include/*.h”

s.preserve_paths = “libCalculator.a”
s.ios.vendored_library = “libCalculator.a”

s.libraries = “stdc++”, “Calculator”
s.requires_arc = true

Test this Sample

I have created a test library named Calculator which calculates a^2 + b^2 + 2ab for a given values a and b. For demonstration purposes, i have internally used c++ class to perform addition and multiplication. This is to demonstrate how to supply additional dependent libraries in the pod spec file.

Here is the github link : https://github.com/bharath2020/CalcLibrary

To test the library with an example project, add below line to your pod file


pod ‘CalcLib’

and execute pod install or pod update


You can insert the following code in your example project for verification

Calculator *calci = [[Calculator alloc] init]

int result = [calci APlusBWholeSquare:2 andB:3]


Here are few references who distributes iOS Static library via cocoa pods:




Hope this Helps.

Leave a Reply

Your email address will not be published. Required fields are marked *