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.
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.
CocoaPods itself is distributed as a Ruby gem, so installing it is as simple as:
[sudo] gem install cocoapods pod setup
Using CocoaPods to manage your project’s dependencies is a two-step process:
- Specify the dependency in you project’s dependency specification file,
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
- In your project folder, create a new file,
- 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'
pod install RestEasyWithCocoaPods.xcodeproj.
- Open the newly created workspace,
- Add the RestKit headers to
#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.