Post navigation

How to install CocoaPods and setup with your Xcode project

Sometimes we are using many Git libraries inside our app. Like JSONKit, AFNetworking, MBProgressHUD, EGOTableViewPullRefresh, Facebook-iOS-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, wasting space.

Sometimes, it’s hard to get a specific version of a library.

There’s no central place where you can see which libraries are available.

Finding a new version of a library and updating your project with it is time taking and sometimes, painful.

Downloading libraries manually creates a tendency to perhaps make some changes to the downloaded code.

Updation is harder.

The most simplest solution is to use CocoaPods.

CocoaPods is the dependency manager for Objective-C and Swift 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.

Installation:

Before installing Pods make sure you have installed/updated command line tool in your Mac machine. You can check this by opening Xcode, navigating the menu to Xcode > Preferences > Downloads > Components, finding Command Line Tools and select install/update.

Also, make sure Git is installed in your Mac machine.

Open terminal and run this command:

sudo gem install cocoapods

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.

If you are getting any error with XCode like developer path is missing. First run this command in terminal: sudo xcode-select -switch /Applications/Xcode.app (or your XCodeName.app)

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-remove-update libraries in pod:

1. Open Terminal

2. Change directory to your XCode project root directory. If your terminal is already running then no need to do this, as you are already at same path.

3. $ touch podfile

4. $ open -e podfile (This should open a blank text file or you can use: open -a Xcode podfile to open this inside Xcode)

5. Add your library names in that text file. You can add new names (lib name), remove any name or change the version. If you are using CocoaPods 1.0 or above you also have to have a ‘target’ section in your Pod File.

NOTE: Use ( control + ” ) button to add single quote at both end of library name. It should be shown as straight vertical line. Without control button it shall be added as curly single quote which will give error while installation of file.

6. Save and close this text file. Now libraries are setup and you have to install/update it.

7. Go to your terminal again and run this command: $ pod install (to install/update these libraries in pod).

You should see output similar to the following:

Updating spec repo `master’
Installing AFNetworking (3.0)…
Generating support filesAfter successful downloading/installation of any single library, It will generate ProjectName.xcworkspace in your project root directory. From now use this workspace to open your project. When you open your project work space you will see Pod is also added as another Xcode project with your Xcode project. If you open Pod it will show all libraries.

For your next project your don’t need to install the cocoa pod again in your mac machine just open the terminal and setup the Pod in root directory of your Xcode project and always remember these 3 commands :

1. $ touch podfile

2. $ open -e podfile

3. $ pod install

To update Pod libraries : $ pod update

To check POD version : $ pod --version
To update CocoaPods : $ gem install cocoapods
or use sudo at start if getting error "You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory."

About Muzammil

Hey Guys, I am Muzammil Mohammad. Having 7 years of experience in iOS development.
I am here to share my knowledge with all mobile application developers and those companies who are stuck on development (due to bad quality of code or don't know what to do) or want to know how to build a good mobile app. I believe I am more expert in UI and Architecture design of mobile app.
So if you need my helps or assistant for your mobile app projects, Feel free to contact me. I will try my best to improve the quality of your app. Or you can hire me to develop your apps. I’d love to talk with you.
Best!

I followed the steps also it was successfully installed….. but after that too when i imported RestKit in AppDelegate.h it is giving me error as RestKit file not found…. Please can you give me any suggestion how to get rid of this error?

Hey Disha, I am still not sure why you getting such issue with RestKit. Only thing i can advise to reinstall POD libraries (specially for Rest Kit). Also, are you using any specific version for POD libraries ? like : platform :ios, ‘5.0’ ? Make sure, Your RestKit lib is supporting your XCode platform version.

Hi Friends,
Thanks For the Simple and Best Tutorial. But Now i need to setup cocoapods for a
workspacce which is having more than five Projects. Any Suggestion for this will be Appreciated. And once again Thanks for You Guidelines.

Hello Muzammil i am facing error with pod install, when i run it shows error as:

[!] Unable to find host target(s) for MAIACoreLib. Please add the host targets for the embedded targets to the Podfile.
Certain kinds of targets require a host target. A host target is a “parent” target which embeds a “child” target. These are example types of targets that need a host target:
– Framework
– App Extension
– Watch OS 1 Extension
– Messages Extension (except when used with a Messages Application)