Getting Started with CocoaPods

CocoaPods is a popular dependency management tool for Objective-C projects. It makes life easier for iOS developers by downloading all the third party libraries you want for your project and linking everything so you don’t have to. It’s a huge time saver and helps you keep all your dependencies up to date.

CocoaPods runs on Ruby which is installed by default with OS X. To install CocoaPods, open Terminal and type the following:

sudo gem install cocoapods

You should see something similar to the following screen:
Screen Shot 2014-04-20 at 5.57.10 PM
Since this is the first time installing CocoaPods make sure to run the following in Terminal:

pod setup

Screen Shot 2014-04-20 at 5.58.22 PM
Just like that you have successfully installed CocoaPods!

Installing Your First Dependency
Open Xcode and create a new Single View Application project and call it DigitalFontExample.
Screen Shot 2014-04-20 at 6.01.15 PM
Now open Terminal, navigate to the folder where you created your project and type the following:

touch Podfile
open -e Podfile

Screen Shot 2014-04-20 at 6.09.52 PM
What this does is create the Podfile for you and opens it in TextEdit. For this example we want to use the FBDigitalFont control. In your Podfile enter the following:

pod 'FBDigitalFont'

In past versions of CocoaPods you also had to specify the platform but this is no longer the case. Save your file and close TextEdit. Now go back to Terminal and enter the following:

pod install

You should see the following output:

Analyzing dependencies
Downloading dependencies
Installing FBDigitalFont (0.0.4)
Generating Pods project
Integrating client project

[!] From now on use `DigitalFontExample.xcworkspace`.

Notice the line that says [!] From now on use `DigitalFontExample.xcworkspace`.
You have to do exactly that. Close your Xcode project and open DigitalFontExample.xcworkspace. You should have something similar to this:
Screen Shot 2014-04-20 at 6.59.41 PM
You have successfully installed your first dependency via CocoaPods!

Testing your dependency
Open ViewController.m and import FBDigitalFont at the top.

#import "ViewController.h"
#import <FBDigitalFont/FBLCDFontView.h>

Now create a setupLCDFont method and call it from viewDidLoad.

- (void)setupLCDFont
    CGRect frame = CGRectMake(20, 150, 300, 50);
    FBLCDFontView *v = [[FBLCDFontView alloc] initWithFrame:frame];
    v.text = @"20141231";
    v.lineWidth = 4.0;
    v.drawOffLine = YES;
    v.edgeLength = 20;
    v.margin = 10.0;
    v.backgroundColor = [UIColor blackColor];
    v.horizontalPadding = 20;
    v.verticalPadding = 14;
    [self.view addSubview:v];
    [v resetSize];
- (void)viewDidLoad
    [super viewDidLoad];
    [self setupLCDFont];

Compile and run. You should see something like this:
Screen Shot 2014-04-20 at 7.30.06 PM
This is how easy it is to install and use third party dependencies via CocoaPods. I hope you found this tutorial useful. I know there are several other tutorials out there but almost all of them are out of date and include steps that are no longer needed. In time, this tutorial will also be out of date but as of now it is probably the most current one available.

Sample project is available on github:

No Comments

You can leave the first : )

Leave a Reply

Your email address will not be published. Required fields are marked *

Please Do the Math      

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: