Using CocoaPods to manage your dependencies
One of the few things that I find really annoying about developing apps for the iOS platform is how cumbersome it is to include third party libraries in your projects. Depending on the complexity of the library (e.g. its respective dependencies and transitive dependencies) and the effort the authors put into the consumability of their library, the steps required to add a library to your project range from just copying a few files into your source folder to a whopping two-page description of drag’n’drop wizardry.
Wouldn’t it be awesome if you could just specify a list of dependencies and be done? You might have heard of RubyGems, Maven, Ivy, NuGet or npm that bring dependency management to the worlds of Ruby, Java, .NET and JavaScript developers.
Well, it turns out there finally is a solution for us Cocoa developers – CocoaPods. Getting started with CocoaPods is really easy and once you started using it, you won’t look back again.
Installation
CocoaPods itself is distributed as a Ruby gem, so installing it is as simple as:
[sudo] gem install cocoapods pod setup |
Usage
Using CocoaPods to manage your project’s dependencies is a two-step process:
- Specify the dependency in you project’s dependency specification file,
Podfile - Run
pod install
Let me illustrate this by way of a simple example. Let’s assume you want to build an application with the awesome RestKit library. If you have used RestKit before, you know that adding it to your project is a rather involved process. Over the years, it has become significantly easier, but it still needs a very detailed installation instruction with lots of screenshots – check out their wiki page.
With CocoaPods, all you need to do is this:
- Create a new empty project, naming it
RestEasyWithCocoaPods - In your project folder, create a new file,
Podfile - Fill in the following information:
platform :ios dependency 'RestKit' dependency 'RestKit/UI' dependency 'RestKit/Network' dependency 'RestKit/ObjectMapping' dependency 'RestKit/ObjectMapping/CoreData' dependency 'RestKit/ObjectMapping/XML' dependency 'RestKit/ObjectMapping/JSON'
- Run
pod install RestEasyWithCocoaPods.xcodeproj. - Open the newly created workspace,
RestEasyWithCocoaPods.xcworkspace - Add the RestKit headers to
RestEasyWithCocoaPods-Prefix.pch:#ifdef __OBJC__ #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> #import <RestKit/RestKit.h> #endif
- Done. Go and build awesome stuff with RestKit (more on this in another post).
Where to go from here
Wasn’t that a lot easier than all the other ways to install third party libraries?
If you are curious which libraries are supported by CocoaPods, go to CocoaPods.org – they have a nice little UI for searching their pod specification repository. If you are more include to using the command line, you are welcome to use pod search <yoursearchterm> to search for a specific library.
If your preferred library is not yet supported, why not help them out and build a pod spec? CocoaPods itself and the pod spec repository are hosted on Github, so adding new pod specifications is really easy. The CocoaPods wiki has a section on creating and publishing new podspec files.
Now go and create awesome stuff!
Thanks for reading this post. Follow me on twitter here to be notified about updates and other posts I write. Or, subscribe to my RSS feed here. If you want to get in touch with me, use the contact form.






Tanks for this informations. I will try to use cocoa pods with restkit and Jenkins.
I have encountered the following error message:
Installing dependencies of a project:
$ pod install
Downloads all dependencies defined in `Podfile’ and creates an Xcode
Pods library project in `./Pods’.
The Xcode project file should be specified in your `Podfile` like this:
xcodeproj ‘path/to/XcodeProject’
If no xcodeproj is specified, then a search for an Xcode project will
be made. If more than one Xcode project is found, the command will
raise an error.
This will configure the project to reference the Pods static library,
add a build configuration file, and add a post build script to copy
Pod resources.
Do we have to download the RestKit files from GitHub prior to running pod install?
Kindly advise.
Thank you in advance.
Ken,
Navigate to the directory where you created the pod file and try running pod install from there…
howard
Howard, Ken,
you need to specify the project name the first time you run pod install, like mentioned in step 4:
$ pod install yourprojectname.xcodeproj
This will create the workspace mentioned above. On subsequent runs, you can omit the project name and just run pod install.