How to setup CocoaPods with a Swift project?

So today, we’ll see how to integrate CocoaPods with a Swift project. In the end of this article, I will also explain how to do the same for an Objective-C project.

Before we started, you would like to spend your 10 minutes on YouTube? See this video on CocoaPods. It’s a good start to understanding the need of integrating CocoaPods in our projects.

What is CocoaPods?

CocoaPods is a manager which handles library dependencies for your Xcode projects. As a iOS developer, you may be aware of CocoaControls, it is having lots of controls developed by developers like your and me and make it available for the community.  The main question, why they have created this control, they might develop it for their app and later on they make it available to the community to help other developers. Obviously, they will get fame for their controls. But they are deserving, as they have spent a good time in writing that code, to maintain it, to avail more updates.

So now, here is the story starts, when you want to develop a new iOS application and checked above site to download 5 libraries which will help your app to develop fast with good features. This is good to adopt a new code but the problems come when that library will get an update and you will have to do it manually. Now imagine, there are 5 libraries already added in your code and you planned to update them one by one, too complicated, right? That’s why CocoaPods introduced.

With CocoaPods, there will be a single file, which can include all the libraries which you want to use in your next Swift application. Whenever there will be an update available you simply need to run a command to update that library for your project. Sounds good, right? Yes, it’s that simple.

The dependencies for your projects are specified in a single text file called a Podfile. CocoaPods will resolve dependencies between libraries, fetch the resulting source code, then link it together in an Xcode workspace to build your project.

Ultimately the goal is to improve discoverability of, and engagement in, third party open-source libraries by creating a more centralized ecosystem.

So now, I am assuming that you have enough set to start with setup CocoaPods with your project.

Install CocoaPods.

Note: You can skip this if you already have CocoaPods installed on your Mac.

You can check whether your Mac already has CocoaPods installed or not. Open the terminal and write this command:

pod install ↵
↵ means an Enter [or Return] key.

If it’s not installed it will give you an error:

-bash: pod: command not found

To install CocoaPods on your Mac:

Open your terminal (or use the same which you have opened before) and write this command.

sudo gem install cocoapods ↵

If you asked for your system password, please enter your password, you will not able to see it on Terminal while typing but enter it and press enter. It should then start the process to install the CocoaPods on your Mac. If you’re facing an issue with the installation of CocoaPods, please add a comment here or you can also check this StackOverflow post for more details.

Note: You muse use sudo once. Later on, you can run any command without it.

After that, you need to write this command to complete the setup:

pod setup --verbose ↵

Hold tight, it will take few minutes.

On successful installation, press Command + N to open a new Terminal window and close the previous one, this may be required for some users. So now you should have CocoaPods installed on your Mac and you’re good to go with integration it in your new or existing project.

The same command can be used to update the existing CocoaPods.

Note: You can directly skip to Step-3 if you want to (setup) integrate CocoaPods in your existing project.

Step 1: Create a New project, name it “TestingCocoaPods” with a Single View application.

Screen Shot 2017-05-26 at 5.40.24 PM

Figure 1. Start a new project.

Screen Shot 2017-05-26 at 5.40.56 PM

Figure 2. We are naming it “TestingCocoaPods”.

Screen Shot 2017-05-26 at 5.42.58 PM

Figure 3. You can see the simple project structure, there’s nothing related to CocoaPods.

Step 2: For this test project, we will integrate Alamofire with CocoaPods.

To find out which version of a particular library is available to integrate you can either read it on their website (if available) or on Github repo or in CocoaPods website.

Note: Not all the libraries are available with CocoaPods.

Click here to see Alamofire on

Step 3: If you have installed CocoaPods with this post then you should already have one Terminal window else open Terminal app.

Screen Shot 2017-05-26 at 6.30.43 PM.png

Figure 4. Terminal

Step 4: Inside the Terminal, Change directory to your new or existing project source directory. You can do this with the following command:

cd <drag and drop your project folder> ↵

Step 5: Let’s create an empty Podfile. In the Terminal window, write this command:

pod init ↵

Podfile is a plain text file to let you specify all the libraries you require for your project.

After running this command go to your source directory you should see a newly created Podfile.

Screen Shot 2017-05-26 at 6.32.05 PM.png

Figure 5. A Podfile is created in your source directory.

Step 6: 5th step should be created an empty Pod file in your project directory. Now open it to add Alamofire. To open a Pod file in edit mode write this command in Terminal.

open -e Podfile ↵

Step 7: When you open a Podfile, you should see the following contents inside the file.

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

target 'TestingCocoaPods' do
 # Comment this line if you're not using Swift and don't want to use dynamic frameworks

# Pods for TestingCocoaPods


Note: You can comment use_frameworks! if you are not integrating it for a Swift project.

So now, it’s a time to add Alamofire inside this Podfile.

Step 8: In the Podfile, add this

pod 'Alamofire', '~> 4.4'

Note: At CocoaPods, every library is referred as a “Pod”, like “Repo” for Github. And they may have a specific version which you can integrate with your project. You can also add the above line without specifying a version.

pod 'Alamofire'

So the Podfile will be like this.

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

target 'TestingCocoaPods' do
# Comment this line if you're not using Swift and don't want to use dynamic frameworks

# Pods for TestingCocoaPods
pod 'Alamofire', '~> 4.4'

Step 9: Save the Podfile and close it.

Step 10: Now again come back to the opened Terminal window. We have specified which Pod (or multiple Pods) we have to integrate with our project so it’s time to install it to our project.

Step 11: Write this command to add Alamofire Pod in your Project:

pod install ↵

    Or to see what's going on in Terminal?

pod install --verbose 


Screen Shot 2017-05-26 at 6.27.15 PM

Figure 6. Pod Install with verbose


If a Pod installs successfully, you should see this message in Terminal.

Screen Shot 2017-05-26 at 6.37.07 PM

Figure 7. Pod installation completed.

Screen Shot 2017-05-26 at 6.32.11 PM

Figure 8. Pods folder, a .lock file, and a xcworkspace file will be added to your source directory.

You should also see this message in your Terminal:

[!] Please close any current Xcode sessions and use `TestingCocoaPods.xcworkspace` for this project from now on.

Everything is successful, close the TestingCocoaPods.xcodeproj and Open TestingCocoaPods.xcworkspace. From now, you should always open “

From now, you should always open “TestingCocoaPods.xcworkspace” whenever you wish to do anything with your project.

That’s it.

Now open TestingCocoaPods.xcworkspace and it should look like this.

Oops, my Xcode is started updating couldn’t open it for now to take a new screenshot. Why not you try it?

Whenever there will be a new update to Alamofire you can simply follow Step-4 and write this command to update it.

pod update ↵

Note: This command should only work if you have not specified a version with your Pod. So if you have Alamofire in Podfile like this, pod ‘Alamofire’ then it can be updated if updates available.

Here’s an article from raywenderlich if you would like to go a bit deep about this. You can check it from here.

Leave a Reply

Please log in using one of these methods to post your comment: 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