An Introduction to CocoaPods

Posted By : Gaurav Kumar | 12-May-2018

Today In this article, we are going to discuss what is CocoaPods and why we need CocoaPods. So let get started by what is CocoaPods. CocoaPods is an application dependency manager for Swift and Objective-C Cocoa projects. It has thousands of libraries and is used in over million of application.

 

The next question is why we need CocoaPods. CocoaPods make easy the life of developer by simply integrating the third library easily into our project. An application dependency manager let us add, remove and update any third library very easily and with less effort.

 

Before CocoaPods, how a developer is managing and integrating third-party library. There are a lot of steps involved in integrating third-party library manually. Let us point out some of the steps. And the steps are given below:

  1. Download and unzip files
  2. Drag framework and bundles to your project
  3. Link with new iOS libraries.
  4. Add Linker flags
  5. Go to step1!

CocoaPods is the superstar of iOS application development. Those people coming from android application development background, they can relate CocoaPods with Gradle. As we know that Gradle is also an application dependency manager and build tool. Those people coming from java application development background, they can relate CocoaPods with Maven.

 

So let get started with today agenda. And the agenda is

  1. Installing CocoaPods
  2. Initializing project for Pod
  3. Adding third party library

Installing CocoaPods :- CocoaPods is built with Ruby and is installable with the default Ruby available on OS X. We recommend you to use the default ruby. Installing CocoaPods is very simple, first, open your terminal in Mac OS. And the command is

 

$ sudo gem install cocoapods

 

Once the CocoaPods is installed, we can check the version of CocoaPods by simply writing pod -version

 

Initializing project for Pod : - The project in which we want to add a third-party library, we must initialize that project for Pod. For initializing that project for installing pods, we must navigate to the project location in our terminal. For example, the project name is Test and the project location is /Users/abc/Documents/iOSProject/Test. So we have to navigate to the project location. Once we are navigated to project location, write ls command to see the files in our project.

 

Since the project doesn’t contain Pod file. Because we didn’t initialize our project for Pod. The command for initializing Pod is

 

$ pod init

 

After above command is executed, then execute ls command to see the Pod file in our project. Since the Podfile doesn’t contain any dependency library by default.

 

Adding third-party library:- Adding third-party library to our project is as simple as easy. Suppose we know the exact name of the third-party library, in my case we are going to use swift networking library. And the library name is Alamofire. To add Alamofire library to our project, open Podfile by writing open Podfile. Once the Podfile is opened, we can write down the name of the library after target line of this file. We have to write down

 

pod ‘Alamofire’

 

Save and exit the Podfile. Next, we have to install the added library by writing

 

$ pod install

 

The above command downloaded the Alamofire with the latest version. We can also specify the version with the library name. For example

 

pod ‘Alamofire’, '~> 2.3'

 

Once the pod is successfully installed for alamofire. We must open App.xcworkspace instead of the project file when building our app. We can check the number of pods installed for a project by opening or viewing Podfile directly. In order to use installed pod in our Cocoa class, we must write down import statement by writing import Alamofire.

 

About Author

Author Image
Gaurav Kumar

Gaurav Kumar is a software developer having skill set in java and mobile application development.

Request for Proposal

Name is required

Comment is required

Sending message..