Installing Libraries Using CocoaPods

Installing Libraries Using CocoaPods

May 26, 2015

There are a lot of software developers worldwide and through the internet we are better connected than ever. Platforms such as GitHub and StackOverflow further improve our ability to share solutions to common problems we face in software development.

Once you start developing software, you will realize that many, if not most, of the problems you encounter have been solved by someone else before. Reusing these solutions in our own projects allows us to build software at unprecedented speeds.

This last step will show you how to easily incorporate third-party code into your iOS projects, before you head on to work on your own project.

We have also prepared a brief lecture that will bring you up to speed on how to use third-party code in your app:

You can download the slides here.

What Is a Dependency Manager?

One way of adding third-party code to your project would be to download all of the source code and copy it into your project.

This may sound straightforward, but in reality this approach will fail for many projects. Many third-party libraries come with their own dependencies and very special rules about how they need to be compiled; information that is not in the source files but in the project file of the library.

Even if that approach works, you will have a hard time updating the library. You will have to remove all code and add it again.

To avoid such kinds of problems, other software developers have built dependency managers. The most popular one for iOS is CocoaPods.

How Does CocoaPods Work?

CocoaPods provides a huge repository of available iOS libraries. When you want to use CocoaPods you add a special file to your project in which you can list your dependencies. That file is called a Podfile.

If you want to use a library, you need to enter the name (and optionally the version) of the library into your Podfile. Then you can use one of CocoaPods's commands to install the library that you listed in the Podfile.

Installing CocoaPods

As a first step, we need to install the CocoaPods software.

Open a terminal and enter the following line:

sudo gem install cocoapods

After a while the installation should complete successfuly!

Setting up A Podfile

Now we can create a Podfile that will allow us to declare dependencies for our project. The CocoaPods tool provides us with a convenient method to do this:

Open the root directory of your project in a terminal and type:

pod init

Now a Podfile with some skeleton content will be created. Open the Podfile in your favorite text editor. It should look like this:

# Uncomment this line to define a global platform for your project
# platform :ios, '6.0'

target 'Makestagram' do


target 'MakestagramTests' do


Installing a Library

Now we are ready to add a dependency to this file. For the feature we want to implement - displaying the amount of time that has passed since a post was created - we want to use the DateTools library. It will help us to convert time spans into human readable strings.

Now how do you find such a library on your own? Google! For example "Date time since iOS library". In many cases your search terms will lead you to a GitHub page as shown below:


That GitHub page will typically contain the name CocoaPod of the library, which allows you to add it your Podfile.

Add the DateTools dependency to your Podfile so that it looks like this:

# Uncomment this line to define a global platform for your project
# platform :ios, '6.0'

target 'Makestagram' do
  pod 'DateTools'

target 'MakestagramTests' do


Then, return to the command line and run:

pod install

Now your dependencies will be download and installed.

The next step is very important. After you have installed your first library, CocoaPods will generate an Xcode workspace for you. When using CocoaPods, you need work with that workspace and no longer with the Xcode project that you have been working with.

Close your Xcode project. Then open the new workspace that CocoaPods has generated: image

Importing the Library

If you have installed a Swift library, you can import it with the regular, well known import statement in any of your Swift files, e.g.:

import MyLibrary

However, most of the Libraries that you will use will be written in Objetive-C, Apple's old default programming language for iOS. These libraries need to be imported a little bit differently.

To import Objective-C libraries, you need to add them to a Bridging Header. Our template project already comes with such a bridging header, so let's add the DateTools library to it.

Add the DateTools library to Makestagram's bridging header as shown below:


Using the Library

Now we can finally use the Library to display how long ago a post has been created! Let's extend the PostSectionHeaderView to use the new library.

Extend the didSet observer of the post property in the PostSectionHeaderView as following:

var post: Post? {
  didSet {
    if let post = post {
      usernameLabel.text = post.user?.username
      // 1
      postTimeLabel.text = post.createdAt?.shortTimeAgoSinceDate(NSDate()) ?? ""
  1. We are reading the createdAt date from the post. This is a property that Parse sets by default on all PFObjects. Then we use an extension provided by the DateTools library: shortTimeAgoSinceDate(_:). This method takes a comparison date. By calling NSDate() we create a date object with the current time. If the post has been created 4 hours ago, this line of code will generate the string "4h". Since createdAt? is an optional, we use the ?? operator to fall back to an empty string, in case the 'createdAt' date should be nil.

Awesome! Now it's once again time to test this new feature. When running the app you should now see that the header cells display how long ago a post has been created:



In this step you have learned how to leverage the power of open source software in your project. There couldn't be a better way to end this Makestagram tutorial!

Now it's time to take everything you have learned and build your very own app! And always remember: some developer might already have solved your problem for you. The answers are at your fingertips!


If you have feedback on this tutorial or find any mistakes, please open issues on the GitHub Repository.

Join Make School Icon

Join Make School

Prepare for your career as a founder or software developer

Apply now
Talk to Us Icon

Talk to Us

If you have any questions, please send us an email.

Email Us