What is CocoaPods?
“CocoaPods: The best way to manage library dependencies in Objective-C projects.”
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:
- No more wasted time downloading all the libraries the one we want to use depends on.
- 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 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:
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 `https://github.com/CocoaPods/Specs.git' (branch `master')
Setup completed (read-only access)
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.
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:
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
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
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!
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! 😀