Managing Dependencies With CocoaPods

Note: This is an older post which was migrated to Ghost.

CocoaPods is a tool which makes managing dependencies for your project a breeze. It’s the equivalent of Bundler for Ruby, but for Objective-C libraries. Using CocoaPods can dramatically simplify your workflow when you’re using external libraries.

Previously, when you wanted to add a library to your project, you’d have to manually clone the repository, add it to your project and set the necessary flags before you’d be able to use it. This can be time consuming if the library doesn’t match the ARC setting of your project, because you’d need to set the special ‘-fno-objc-arc’ or ‘-fobjc-arc’ flag on each of the files.

Installing Cocoapods

CocoaPods is written in Ruby and needs to be installed via the ‘gem’ command. Your mac should come with Ruby pre-installed, but you can always install ruby via Ruby Version Manager.

gem install cocoapods  

This can take a few seconds. After it’s done you should be able to access the 'pod' command and see a list of possible arguments.

➜ pod

To see help for the available commands run:

  * $ pod install --help
  * $ pod list --help
  * $ pod outdated --help
  * $ pod push --help
  * $ pod repo --help
  * $ pod search --help
  * $ pod setup --help
  * $ pod spec --help
  * $ pod update --help

Options:

    --help       Show help information
    --silent     Print nothing
    --no-color   Print output without color
    --verbose    Print more information while working
    --version    Prints the version of CocoaPods

You should now run ‘pod setup’, which will create or update the repository containing the specs for each of the supported libraries.

➜ pod setup
Setting up CocoaPods master repo  
Updating spec repo `master'  
Setup completed (read-only access)  

Now you’re ready to start using CocoaPods.

Searching for a Pod

Not every library is available as a pod, but more libraries are being added each day. In fact, if a library you need isn’t available as a Pod, you could easily create one yourself with the ‘pod spec’ command. Use the –help flag for more info.

Let’s say we want to add the AFNetworking library to our project. You can see if it’s available by searching for it:

➜  pod search afnetworking

-> AFNetworking (1.0)
   A delightful iOS and OS X networking framework.
   - Homepage: https://github.com/AFNetworking/AFNetworking
   - Source:   https://github.com/AFNetworking/AFNetworking.git
   - Versions: 1.0, 1.0RC3, 1.0RC2, 1.0RC1, 0.10.1, 0.10.0, 0.9.2, 0.9.1, 0.9.0, 0.7.0, 0.5.1
   [master repo]

The results tell us that AFNetworking is in fact available as a Pod through CocoaPods, as well as which (previous) versions are available.

Adding a Pod to your project

Time to add the library to our project. Create a file called ‘Podfile’ inside your project directory with the following contents:

(If you’re working on a mac project, you should or course choose the mac platform.)

latform :ios, '5.0'

pod 'AFNetworking'  

Alternatively, similar to the way Bundler works, you can also specify a minimum version, like so:

pod 'AFNetworking', '~> 1.0'  

Or a specific version, like so:

pod 'AFNetworking', '1.0'  

When you want to install the libraries into your project, you need to execute the ‘pod install’ command from inside your project directory. This will clone the necessary repositories and combine them into a static library (libPods.a), which will be added to your project. It will also create a new workspace for your project, which you should use from this moment on.

➜ pod install
Updating spec repo `master'

Installing AFNetworking (1.0)  
Generating support files  
[!] From now on use `Project.xcworkspace'.
-> Integrating `libPods.a' into target `Project' of Xcode project `Project.xcodeproj'.

Just open the newly created .xcworkspace file and proceed as you would normally. Since we chose to add AFNetworking, we could now import it’s header files and test if everything is working. To do so, we’ll add some code our AppDelegate.

#import "AppDelegate.h"
#import "AFJSONRequestOperation.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://search.twitter.com/search.json?q=afnetworking"]];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSLog(@"%@", JSON);
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"%@", [error localizedDescription]);
    }];
    [operation start];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    return YES;
}

Because the call is asynchronous, it may take a moment or two before it pops up, but you should see the parsed response in your console.

That’s all there is to it. If you have any questions, please don’t be shy to post them in the comments.

Erik van der Wal

Erik van der Wal

I love building things with Swift, Objective-C and Ruby and tinkering with technologies like Golang and Elixir.

comments powered by Disqus