CocoaPods 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.
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.
Now 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
Now, install cocoa pods by typing below command in Terminal
Integrating Cocoapods with an Xcode project
sudo gem install cocoapods
Create new Xcode project. (Refer image 2)
Open Xcode -> File -> New -> Project - > Select Single View Application
After creating the project your project structure is look like below image (Refer image3).
Close 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:
The simple editor open in command line add your dependency here. for example:
pod 'SVProgressHUD', '0.9'
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:
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.
Upon success of the 'Pod install' you will get below result (Refer image 5).
Your 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.
if 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.
open 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:
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.