Dependency Management Using Cocoapods

Cocoapods is a Swift and Objective-C dependency management tool like NPM is for Node and Gem is for Ruby. Swift now has its own official package manager. But it’s still very new. Until it matures fully you can keep using Cocoapods.

Installing Cocoapods

Use Gem to install Cocoapods

sudo gem install cocoapods

A few things may go wrong.

You may see this error:

ERROR:  While executing gem ... (Errno::EPERM) Operation not permitted

If you see that try installing Cocoapod like this:

sudo gem install -n /usr/local/bin cocoapods

You might see a Insecure world writable dir warning. This can be safely ignored.

enter image description here

Declaring Dependencies

Create an Xcode project if you don’t already have one. Here, we will assume that the name of your project is MyApp. In the root folder of the project create a file called Podfile.

platform :ios, '9.0'

target 'MyApp' do
    pod 'AFNetworking', '~> 2.0'
    pod 'FLKAutoLayout'

This states that our project requires AFNetworking version 2.0 or higher. It also depends on the latest version of FLKAutoLayout.

Installing Packages

When you install a package Cocoapod downloads the source files of the package and any other packages that it depends on. Cocoapod creates a new project called Pods and adds these source files there. It then creates a new workspace and adds your project and the Pods project to the workspace.

Since Cocoapod modifies your Xcode workspace, it is better that you shutdown Xcode while you are installing a package.

Make sure that your Podfile is properly defined. From the root folder of your project run:

pod install

enter image description here

There are a few things you have to do for your project to compile correctly:

  1. Shutdown Xcode if it is running.
  2. Double click the workspace file to open it and not the project file. In our case that will be the MyApp.xcworkspace file.

enter image description here

Now you will see the Pods project added to the workspace. Source code for the downloaded packages will be there.

enter image description here

Cocoapod will also modify your project to link in the code from all packages in the Pods project.

You are now ready to use classes from these packages.

Removing Packages

To remove a package simply delete it from Podfile. Then from the root of the project run:

pod update

Using Pod Packages in Unit Tests

So far in this article we have added packages only to the application’s main target. If you try to use these packages from unit tests you will get a compilation error. The solution is to also add the packages to the unit test targets. Find out the name of the unit test target and specify the packages used from there.

platform :ios, '9.0'

target 'MyApp' do
    pod 'AFNetworking', '~> 2.0'
    pod 'FLKAutoLayout'

#Add any package you need to the unit test target
target 'MyAppTests' do
    pod 'AFNetworking', '~> 2.0'
    pod 'FLKAutoLayout'

Run pod update to set things up.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

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