CocoaPods 101

Vasudha Jha
The Engineering Gecko
5 min readNov 26, 2019

WHAT IS COCOAPODS?

According to CocoaPods.org,

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over 68 thousand libraries and is used in over 3 million apps. CocoaPods can help you scale your projects elegantly.

This means that it helps us incorporate and manage third party libraries which we can use effortlessly in your project.

But before we try to see it in action, let’s understand why we should use third party libraries in our apps and why should we use CocoaPods to integrate them in our project.

WHY DO WE NEED THIRD PARTY LIBRARIES?

We can look at two of the core software engineering principles that we apply in our day to day work:

  • DRY (Don’t Repeat Yourself)

This acronym implies developing modular, reusable and configurable components in any application instead of code duplication.

  • Don’t reinvent the wheel

This encourages developers to avoid spending time and effort on problems which have already been solved and they can focus on new problems at hand.

The most common way of putting these principles into practice for a developer is to use third-party libraries.

WHY USE COCOAPODS?

Integrating third party libraries into your project is not always an easy task.

Let’s look at series of steps that we would have to go through if we did not use cocoaPods:

Created using canva

The fifth step means that if the third party library that we want to use depends on other third party libraries, we would have to repeat the steps 1 through 4 again. Yes, I know what you guys are thinking.

This is exactly where CocoaPods comes into picture.

The author of a library is going to write a Podspec file which specifies everything that the developer integrating the library needs to know. It includes details about where the source should be fetched from, what files to use, the build settings to apply, and other general metadata such as its name, version, and description.

For example:

Podspecname = "StrawberryPod"
version = "2.5.4"
source_files = "Strawberry/Library/*.h"
source_location = { :git => "<<Github Link>>"}

As a developer consuming a library, you do not need to care about most of the things on the file but the name of the pod and optionally the version, if there is a specific version that you want to use.

But, where does the name and version in the Podspec file go?

We need to create a new file in our project directory called the Podfile which will contain the URL of the PodSpecs representing the libraries that we want to use in our project.

Now that we have answered most of the questions, let’s start using CocoaPods!

1. Install CocoaPods

We will be using the RubyGems tool to install CocoaPods.

Yes. You heard it right.

A dependency manager to install another dependency manager.

So open your terminal program and type in the following command:

sudo gem install cocoapods

This step might take some time, and your system might look like it’s hanged, but be patient.

2. Setup CocoaPods

Run the following command:

pod setup

This command will download every single Podspec file from official CocoaPods repository to your machine.

SideNote:

If a library gets updated in the future, the Podspec files automatically update too, hence this is only a one time process.

3. Create a simple project in Xcode and then close it.

4. Create Podfile

Navigate to your project directory in the terminal where we will create our very own Podfile!

Run the following command:

touch Podfile

Alternately,

You can run the command below which will create a slightly filled out version of the Podfile.

pod init

5. Edit your Podfile

We need to tell CocoaPods what platform you’re running it on and it’s deployment target (the minimum target that your app is going to support) like so:

platform :ios, '7.0'

Why do we need to specify the deployment target?

Some libraries might work only on some versions of iOS. Thus, by telling CocoaPods the oldest version of iOS that your project is going to support, you are helping it to provide the dependencies that are compatible with your version.

Next, you need to add the Pods needed for your project.

Say we want to add the AFNetworking library, but we don’t know what it’s Pod is exactly named.

We can find that out in two ways:

Search the command line:

pod search AFNetworking

We see a bunch of results, and the first one looks like the one we need.

Sidenote:

What are the rest like AFNetworking+AutoRetry that pop up after the search?

They are categories written by other developers to extend the capabilities of a library.

Or,

Go to cocoapods.org and search for the library:

Now that you know the name of the Pod, add it to your project:

pod 'AFNetworking'

6. Install the Pods

Go back to the terminal and type the following command:

pod install

This command will fetch all the libraries that we need and install them.

7. Open the project workspace in Xcode and build it from there

We are talking about the .xcworkspace file here.

Once the libraries are installed, a file with .xcworkspace extension gets generated.

An xcworkspace is a collection of projects. In our case it will create our basic project and the Pods projects which builds all of our CocoaPods as static libraries.

CocoaPods alters our project to rely on the static libraries in the other project.

This is a really big idea.

Hence, if you try to open the .xcodeproj file instead of the .xcworkspace and try to run it, you’ll get linker errors since Xcode won’t know what these libraries are!

8. Open your ViewController.swift and import the library in there.

import AFNetworking

Tada! You’re all set!

You can now use all the functionalities that AFNetworking offers in your project.

Sidenote:

If you want to check whether you have the latest pod versions for the libraries that you’re using, you can type the following command in the terminal. CocoaPods will check if the newer versions of any pods are available, and it there are, it’ll update them for you.

pod update

To learn more about CocoaPods, check out the CocoaPods documentation.

Also, if you want to go deeper and understand how CocoaPods works under the hood, check out this blog.

--

--

Vasudha Jha
The Engineering Gecko

An engineer, artist and writer, all at the same time, I suppose.