How to install CocoaPods & intro of cocoa pods

KRUNAL TAILORFreelance Mobile Developer
Published:
Updated:
cocoapods iconCocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with CocoaPods. For get in to deep in cocoa pods you must have prior knowledge of the Xcode and command line Tool.

Why?

When working with third party libraries for iOS and OS X project, its hard to manage it manually. CocoaPods makes managing a project’s dependencies much easier. You only need to specify which dependencies, or pods, you want to include in your project and CocoaPods takes care of the rest.

How to install CocoaPods

Cocoapods runs on Ruby, to install & manage it easily. MAC OS X all version by default installed the Ruby. Before starting make sure you installed command line tool from Xcode preference menu. Refer the below image 1 for Xcode preference menu.

Image 1Now you need to do is update RubyGems. For that open the Terminal from Launchpad > Utilities > Terminal on MAC. Type the following command in Terminal:

 
sudo gem update --system
                      

Open in new window


Now, install cocoa pods by typing below command in Terminal

 
sudo gem install cocoapods
                      pod setup
                      

Open in new window


Integrating Cocoapods with an Xcode project

Create new Xcode project.  (Refer image 2)
Open Xcode -> File -> New -> Project - > Select Single View Application

Image 2After creating the project your project structure is look like below image (Refer image3).

Image 3Close the Xcode project.  Now we will go for install our first dependency for project. There are so many third party library available  on cocoa pods but for this tutorial we will use SVProgressHUD - A awesome library to create custom activity HUDs for iOS Application, More information about this library you will get here( https://github.com/samvermette/SVProgressHUD).

Now open terminal and change directory to your project directory. Enter below command in terminal:

 
vi Podfile
                      

Open in new window


The simple editor open in command line add your dependency here. for example:

 
pod 'SVProgressHUD', '0.9'
                      

Open in new window


A pod is keyword followed by the name  of library. You can add optional version number for library. Here '0.9' indicates the version no , if you left the version number empty CocoaPods will use the latest version available. Now hit the below command in terminal:
 
pod install
                      

Open in new window

 

Common Error - Be careful Here.
If you didn't put your Podfile with your .xcodeproj file you will not get success to get the library via pod. Below error you will get (Refer image 4), so make sure your Podfile is with the .xcodeproj file.

Image 4Upon success of the 'Pod install' you will get below result (Refer image 5).

Image 5Your project directory structure is look like this (Refer image 6). Here you get the new .xcworkspace file. From now onwards we will use this file to open the project.

Image 6if you not open the .xcworkspace and you open normal  .xcodeproj file, it will shows you below error (see image 7) on compilation. To avoid it just drag the Pods project into Xcode from your project directory structure. If you directly open the .xcworkspace then no need to drag & add the Pods into Xcode.

Image 7open MyCocoapods.xcworkspace to continue and test if it working or not. Thats it you get your third party library for your project.

Now, suppose your project already contains the pod file and you wan't to add one more third party library in your project then what? Answer is very simple, browse to your pod file in project structure  in terminal. Edit the pod file by hitting vi command in terminal. Add your require dependancies and hit command again in terminal:

 
pod install
                      

Open in new window


If  your third party library depends on another library, then pods will download and installed that dependent library too. So, thats too handy, no need to worry about dependencies. This make cocoa pods to easy to use for  developer.

Ref:
http://cocoapods.org/
https://github.com/CocoaPods/CocoaPods/wiki
1
4,183 Views
KRUNAL TAILORFreelance Mobile Developer

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.