CocoaPods is the dependency manager for Objective-C projects. It has thousands of libraries and can help you scale your projects elegantly.

First foray into CocoaPods

I’m working on updating an enterprise iOS application.  As part of that update, I eliminated dependancies on several third party libraries, and the remaining libraries I wanted to manage using CocoaPods.  Otherwise, the situation was that the source code for those libraries had just been dumped into the project several years ago, and there was no easy way of knowing what version of the libraries was being used nor a sane way of keeping the libraries updated. Using CocoaPods was surprisingly easy, but there were a few gotchas I ran into since I was integrating this into an existing project.  I thought I would share in case there are any other newbies out there who run into similar issues.  Note this isn’t meant as a detailed tutorial, for that I recommend you visit the links at the end of this post under “References/Resources”.

Step 1: Install CocoaPods

CocoaPods is built on Ruby. So it turns out, Ruby is included in MacOSX, and you don’t even need to update to the latest version or anything; in fact CocoaPods recommends you use the standard Ruby install.  In this respect it’s a lot simpler than dealing with some Java projects, where you need to update your JVM, deal with multiple JVM’s on your machine, updating your classpath, etc.  Literally all you need to do is open terminal and type in one line:

$ sudo gem install cocoapods

Step 2: Create a Podfile So for this step, I wasn’t sure what my Podfile should look like, e.g. should I assume a file extension of .podfile, etc.?  So the safest thing was to let CocoaPods create it for me.  This can be done using the “touch” command.  In terminal, cd to your project root directory and type

$ touch Podfile

Now for me, this created an empty Podfile, so I had to fill in the required content manually. In my case, this meant adding this as the first line in the Podfile:

platform :ios, :deployment_target => "8.0"

The deployment target should match your project settings in Xcode.

Step 3: Add the libraries you want to manage with CocoaPods

Go to CocoaPods.org and search for the library you want to use, then copy/paste the library name into your Podfile.  For example, for Google Analytics I added:

pod 'GoogleAnalytics-iOS-SDK'

save your Podfile and then run

$ pod install

From now on, whenever you open your project in Xcode, make sure you are opening the workspace file (.xcworkspace) and not the project file (.xcodeproj).  This way Xcode will properly include CocoaPods and find all your library dependancies. At this point, in theory you should be good to go.  In my case, though, I still couldn’t get my project to compile.  I was getting a “library not found for -lPods…” error.  I resolved it by going into the project Build Settings and setting the option for Build Active Architecture Only to “Yes”.

References / Resources:


Comments

Leave a Reply