CocoaPods makes Xcode External Libraries as Sweet as Chocolate

So you want to import external libraries and frameworks into Xcode really quick and painless? Having a smart Xcode external library dependency solution would be great. Maybe even something cool like RubyGems? Xcode, meet CocoaPods!

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

Traditionally when adding an external static library in Xcode, it’s a pain.

  • Add static library to project (option-click Add Files to Project… or drag file into Xcode)
  • Navigate to project’s target Build Phases tab
  • Add a Target Dependencies to the library
  • Add a Link Binary with Libraries to the library (under “Workspace” folder)
  • May have to add a Header Search Path in the Build Settings -> Search Path
  • If all goes well, you should see the static library appear as a nested project in your main project.

It’s messy. It’s annoying. It should be simpler.

CocoaPods takes care of everything for you, even creating a workspace that holds all the external libraries, and automatically adds them to your Xcode project.

Install & Configure

Let’s get started! Open up good ‘ole Terminal and install CocoaPods:

$ sudo gem install cocoapods

Once that’s finished, make sure you run:

$ pod setup

Podfile

Now you’re ready to go! Navigate to the project root folder, where your .xcodeproject lives, using Terminal. Now create a new file “Podfile” (no extension).

$ touch Podfile

This file is similar to a Gemfile from Ruby, and simply lists all your dependencies and preferred version including the platform you’re developing in, and the minimum version.

A Podfile should look something like this:

platform :ios, '6.0'
pod 'JSONKit',       '~> 1.4'
pod 'Reachability',  '~> 3.0.0'
pod 'AFNetworking'

platform – adding the default version is optional (see the CocoaPods Wiki)

pod – pod libary name, followed by the version (syntax here)

If you’re a stranger to Gemfiles and Podfiles, you should definitely check out the CocoaPods Github Wiki.

Now, let’s install the dependencies. To play it safe, close Xcode if it’s open, especially if the Xcode workspace hasn’t been created yet.

$ pod install

This will install all the dependencies, create a Pods Xcode project and create an Xcode workspace with both Pods and your Xcode projects. From now on, open the Xcode workspace instead of the project file, since that’s how the Pods project is referenced. You may have seen this in the output:

[!] From now on use `[ProjName].xcworkspace'.

You may have noticed a Pods folder was created in your project directory. CocoaPods uses this directory for pod library information and storage. Also, there is also a Podsfile.lock. You can ignore this file. Pods uses this file to determine currently used versions and pod information.

Using Libraries

After opening your Xcode workspace, you will see the libary added to your Xcode project. Add the library to one of your .m code files, and go ahead and play with the library.

#import <AFNetworking.h>
...
NSURL *url = [NSURL URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"App.net Global Stream: %@", JSON);
} failure:nil];
[operation start];

To add another lib or change versions, simply change the Podfile and run again:

$ pod install

CocoaPods Utilities & Docs

You can even search for pods using:

$ pod search [SEARCH QUERY]

To lists all pods available:

(adding --update gets the latest list before showing)

$ pod list --update

Check out the CocoaPods main site which has pod searching, and their Github Wiki, and who can forget trying:

$ pod --help

If you’re getting frustrated incorporating common libraries into Xcode, you will definitely want to give CocoaPods a try. While CocoaPods might not have everything, it’s a great step, and an awesome way to share and develop common libraries as a community.

Matt

Written by Matt

3 Comments

  1. avatar
    web design websites
    May 12, 2013

    What’s up, I check your blogs on a regular basis. Your writing style is awesome, keep up the good work!

    Reply
  2. avatar
    graphic design projects
    July 31, 2013

    Hey! I know this is kind of off topic but I was wondering if you knew where I could get
    a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having problems finding one?
    Thanks a lot!

    Reply

Leave a Reply

*