Thursday 22 October 2015

How to install CocoaPods and setup

Install CocoaPods and setup with your Xcode project

CocoaPods is a dependency management tool for iOs and OS X development.
It makes managing third part libraries in an Xcode easy and straightly.
CocoaPods will resolve dependencies between libraries, fetch the resulting source code, then link it together in an Xcode workspace to build your project.

Why Should we use CocoaPods.

Sometimes we are using many Git libraries inside our app. Like JSONKit, MBProgressHUD, Facebook-SDK etc. But it is very boring to build all libraries or if you add the code in your project, it is tough to manage. Also, there are many disadvantages in this: 
  • Code that could be somewhere else is stored in your repository, that is wasting space.
  • Sometimes, it’s hard to get a specific version of a library.
  • Finding a new version of a library and updating your project which makes time taking and sometimes harder to update.
  • Downloading libraries manually creates a tendency to perhaps make some changes to the downloaded code.
The most simplest solution is to use CocoaPods.

CocoaPods is the dependency manager for Objective-C projects. It has thousands of libraries and can help you scale your projects elegantly. CocoaPods is built with Ruby and is installable with the default Ruby available on OS X.

Install CocoaPods

Before installing CocoaPods you have to make sure bellow items has to be installed/updated.
  • Command Line Tool
  • Ruby (sudo gem update --system)
  • Git
Open terminal and run this command:
sudo gem install cocoapods -V(-v prints detail download info).
Enter admin password. This could take a while. After few minutes it will show green message is cocoa pods installed successfully in your mac machine. 

Hurrah….You successfully installed CocoaPods in your mac machine. Now you can setup Pod with your Xcode project. 

How to setup POD for your Xcode project:

  1. Open Terminal
  2. Change directory to your XCode project root directory (where your ProjectName.xcodeproj file is placed).
  3. Before we create pod file close xCode project.
  4. $ pod setup : (Setting up CocoaPods master repo)
If successful, it shows Setup completed(read-only access). So, you setup everything. Now Lets do something which is more visible…Yes ! Lets install libraries in your Xcode project.

Steps to add-update libraries in pod:

  1. $ touch pod file or pod init (This will create a default Podfile for your project. The Podfile is where you define the dependencies your project relies on. Make sure to create the Podfile in the root of your Xcode project.)
  2. $ open -e podfile (This should open a blank text file)
  3. Add your library names in that text file. You can add new names (lib name), remove any name or change the version e.g: 
    pod 'SVProgressHUD', '0.9'


    OR
    pod 'SVProgressHUD' 
    pod 'Reachability'

    You can even tell CocoaPods what source to use by specifying the git repository or referring CocoaPods to a local copy of the library.

    pod 'SVProgressHUD', :git => 'https://github.com/samvermette/SVProgressHUD'
    pod 'ViewDeck', :local => '~/Development/Library/ViewDeck'

  4. Save and close this text file.
  5. $ pod install
    You should see output similar to the following: 

    Analyzing dependencies 
    Downloading dependencies 
    Installing SVProgressHUD (0.9)
    Installing Reachability 
    Generating Pods 
    project Integrating client project 
    It might also tell you something like this: 
    [!] From now on use `ProjectName.xcworkspace`. 

    VERY INPORTANT!From now on, as the command-line warning mentioned, you must open the workspace not the project.

























  6. $ pod update(If one of your project's dependencies received a major update and you want to use this update in your project, then all you need to do is update your project's Podfile and run pod update on the command line. CocoaPods will update your project's dependencies for you.)

Command Line:

The CocoaPods gem has many more tricks up its sleeve. Even though you can use the CocoaPods website to browse the list of available pods, the CocoaPods gem also lets you list and search the available pod specs. Open a new Terminal window and enter the command pod search result to search for libraries that include the word progress. The advantage of searching for pods using the command line is that you only see the information that matters to you, such as the pod's source and the available versions.
Run the pod command (without any options) to see a complete list of available options. 
More information along with the installation guide can be found at:
Extensive documentation on everything regarding Cocoapods can be found on their github wiki