mokacoding

unit and acceptance testing, automation, productivity

CocoaPods!

What is CocoaPods?

CocoaPods: The best way to manage library dependencies in Objective-C projects.

If you're familiar with Ruby on Rails, it's the same thing as Bundler, or it's lame copy attempt for Symfony 2, Composer.

If you're not, and you haven't sorted it out from the quote above, CocoaPods is a tool that's let us manage our libraries and their dependencies in our Objective-C projects. This means:

  1. No more wasted time downloading all the libraries the one we want to use depends on.
  2. Smart and safe version management, specially when we're working on a project with other people, which is 90% of the time.

To "get my hands dirty" with CocoaPods I made this little project called JustNineGags, feel free to check it out on GitHub.

Why should I use it?

Because it's awesome! It makes development faster and easier, and also safer! It easier to work in teams and keep the libraries versions even. Should I go on? Ok! Just think about this: you won't download and move in your project a library anymore, pod will do it all for you!

Installing CocoaPods

Installing CocoaPods is as simple as installing all the other Ruby Gems, I shouldn't even writing this, as what's written in the install section is more than enough, anyway:

gem install cocoapods

Once the installation is completed run:

pod setup

This will, guess what, setup everything CocoaPods needs on your system. You should see an output like this:

Setting up CocoaPods master repo
Cloning spec repo 'master' from '<a href='https://github.com/CocoaPods/Specs.git'>https://github.com/CocoaPods/Specs.git</a>' (branch 'master')
Setup completed (read-only access)

Done! :)

You should avoid using sudo otherwise everything else you'll do with pod will need to use sudo as well. And this mean that the folders and file that are gonna be created will be owend by root instead that by you.

Using CocoaPods

Again, everything written on the website is pretty straightforward.

Go in the root folder of your Objective-C project and create a file named Podfile, with whatever editor you like. We'll use this file to list all the libraries, pods, we need in the project. The JustNineGags Podfile content is:

platform :ios
pod 'MBProgressHUD', '~> 0.5'
pod 'Reachability',  '~> 3.1.0'

Adding a Pod

As you can see adding a Pod is really easy, just go on CocoaPods website, look for the it, and then add it to the Podfile using it's name and the version you need.

Installing the Pods

Right now we've told CocoaPods the Pods we need but they aren't yet in out project. So let's run

pod install

This will download all the libraries we've asked for, and all their dependencies. Sweet!

The first time we run pod install something else will happen, a Pods/ folder, a Podfile.lock, and a YourProjectName.xcworkspace will be created.

Important! From now on remember to open your project through the YourProjectName.xcworkspace file, otherwise the pods won't be loaded by Xcode.

That's all folks! :)

What should we track?

Using CocoaPods adds some files and folders to our project, which of those should we track in our repo, and which should be left aside, adding them to the .gitignore? That of course assuming you're using git, and you definitely should. Let's have a look at the new stuff:

  • Podfile, we definitely need this one, as all the pods we need are listed in it.
  • Podfile.lock, as for all the other library management systems, we need this one too, because it's used to assure all the developers are using the same versions of the pods and their dependencies.
  • Pods/, we don't need to track this folder, it's created by pod install, and all it's content is downloaded for us from other repos.
  • YourProjectName.xcworkspace, we don't need this one either, because it's generated by pod install too.

What's coming next?

How to setup our own pods. I'll probably write a little and simple Category to add other colors to the UIColor factories, stay tuned!


Update 2013-01-06

To implement HTTP requests in JustNineGags I used SMWebRequest because I'm too lazy to write everything by myself. SMWebRequest wasn't a Pod yet so I opened an issue asking good guy nfarina to add it. In less than 12 hours the Pod was added! :D

Want more of these posts?

Subscribe to receive new posts in your inbox.