Getting Started With CocoaPods

How familiar is this scenario:

You show up for work one morning, filled with optimism and brimming with anticipation at the prospect of all the amazing code you are going to write in the next 9 hours. Ones and zeroes will be bullied into submission, incredible feats of logic will bend the very fabric of reality and the world will be vastly improved by the helpful and elegant programs you will create. Wealth, beyond the dreams of avarice, are just one App Store submission away.

You make your morning cup of coffee, wiggle the mouse back and forth a couple of times to wake your sleeping computer and crack your knuckles. You pull the latest code from the VCS, already thinking of the first line of code you will write. Your fingers, hovering just inches above the keyboard, tingle with anticipation. You are like a thoroughbred in at the gate, awaiting the starter’s bell. Your IDE loads the project. You are ready to code.

But then the unexpected happens… red, squiggly lines of death pop up in open implementation files and errors fill the message log. You check the VCS commit log and see that your nemesis, err, “colleague”, added a fancy pants library from GIT to the project that he just had to have to bring his vision to life. To your horror, you see that he forgot to add the files into VCS. Firing up Skype, your heart sinks as you see that this fiend in human form’s last message was “Leaving on an African Safari… see you in six months!”

Your gentle spirit is crushed beneath the weight of a thousand unresolvable dependencies as you realize that it is going to be a very long morning. Your mind, contemplating the soul destroying prospect of the next 4 hours, turns to the image of the snake devouring itself. You know now that you are caught in a cycle of eternal return, doomed to repeat the same sisyphean exercise for the rest of your career.

Or are you?

Enter Package Management… from NuGet to RubyGems to APT, there seems to be a flavor of package management for every platform and Cocoa is no exception. Gaining support in the cocoa development communities, many of the libraries that FTW use on a regular basis have official support, such as MapBox, while others still are expected to in the future, such as Parse. But even those libraries lacking official support have packages (or ‘pods’) maintained by the community. XCode 5 will even have a cocoapods plugin.

Here’s how I got started with cocoapods:

OPTIONAL – Even though Macs come with Ruby installed, it uses a fairly old version. I recommend upgrading and using RVM. An easy way to do this is via RailsInstaller

It will install Ruby 1.9.2, Rails 3.2 and, more to the point, RVM. Its easy.

1) Assuming you have followed my sage advice and upgraded Ruby and installed RVM, to install CocoaPods, run the following in the terminal (f you are not using RVM, you will need to run the command using sudo):

$ gem install cocoapods

TIPS FOR YOUNG PLAYERS: CocoaPods is under active development and new releases are a regular occurrence. To update, run the following command in the terminal, using sudo ONLY if you are not using RVM:

$ gem update cocoapods

2) Run the following command in the terminal window:

$ pod setup

CocoaPods is now installed and configured! Go buy yourself an ice cream sundae for your efforts.

3) Pods are managed through a file called a ‘Podfile’. At its simples, a Podfile could look as simple as something like this:

pod 'ObjectiveSugar', '~> 0.9'

This would add the pod ObjectiveSugar to your project. It also uses the optional version specifier, in this case version 1.0. If you do not specifiy a version, it will install the latest available.

TIPS FOR YOUNG PLAYERS: The version specifier in the example above would install the latest possible version of the app where the version is 0.9.x. It would not install 1.0. If you are feeling bold, you could write the following to get the head of the repository.

pod 'ObjectiveSugar', :head

4) Save your Podfile in the top level of your XCode project and be sure to call it ‘Podfile’. Now run the following line:

$ pod install

This does several things: In addition to downloading all required dependencies, it creates an xcode workspace file, it adds your project to the workspace and makes several configuration changes to a newly created target.

5) Hopefully, you saw your dependencies being installed magically. Now, to use your CocoaPods with your project it is of uptmost importance that, when opening your project in XCode or AppCode, you DO NOT open the xcode project file, but rather open instead the newly created workspace file. This is what we will develop and debug in, and this is what we will build from. For more information on XCode workspaces, read this:

Having opened the workspace in our IDE, we can now import our dependencies and start coding:

#import <ObjectiveSugar/ObjectiveSugar.h>

For more info and to find CocoaPods packages, visit your local library… or better yet, go to the CocoaPods page itself.

For installation troubleshooting, check their handy guides.

There are Pods for these perennial favorites:

TestFlight Parse MapBox UrbanAirship

Uninstalling Pods couldn’t be easier: Simply remove the pod from the podfile and run ‘pod install’ again. Updating pods is much the same… simply re-run pod install.