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)
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. $ 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-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.
target '<Your Target Name>' do pod 'AFNetworking', '~> 3.0' pod 'FBSDKCoreKit' pod 'JSONKit' pod 'MBProgressHUD' end
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 files After 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."
How to use Pod with Swift? Follow This.
Still facing any issue with Pod? Or want to be in touch with us? Contact us:
Email: muzammil@triffort.com Skype: muz.iphonedev
Triffort Technologies Pvt Ltd
www.triffort.com
Awesome and simple steps. Worked for me .
You are my inspiration , I have few web logs and very sporadically run out from to brand.
One of the simplest and clean tutorials I followed. Thank you!
Its really very very helpful for me.
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?
Restart your XCode and use .xcworkspace project file only.
I did what you suggested…. But it is still giving me same error….. can you suggest me anything else?
Are you able to see POD xcode project in your original project ? Is this showing all libraries which is added in your POD ?
yes… Sir I can see the pod file also the files included in it.. but still I have been facing the problem of RestKit not found
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.
Import your RestKit file like this
#import
not like
#import “RestKit.h”
Thank you
Hi Friends,
i open my terminal and type or install
sudo gem install cocoapods
Iam facing this problem
Error installing cocoapods:
activesupport requires Ruby version >= 1.9.3.?
Which version of Xcode you are using? Ruby was missing in Xcode 5.0 and it seems you are not using latest version of Xcode, so Ruby is also showing old version.
Thanks alot! very simple and smooth. There was an issue with the latest version of gem, I had to switch to gem v 0.19.2 to get it to work. But, in the end it all worked smoothly.
when i am trying to add podfile, i am getting this error.
[!] Oh no, an error occurred.
It appears to have originated from your Podfile at line 1.
Thanks a lot Muzammil, finally i fixes using following steps
i added these lines in podfile
platform :ios, ‘7.0’ (use control + ” to add single quotes. It should be straight)
pod ‘Braintree’
Thank you
Great. Happy to help.
I am followed its working . Thanks a lot …
No problem.
Nice and clean tutorial easy to understand
Thank you.
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.
Hi,
You can add cocoapods for a single project, then add your project xcode file in original workspace.
-Muzammil
simply ***AWESOME*** 🙂
Thank you.
i followed all this but am stuck at “pod setup” it stands near “Setting up Cocoapods master repo” and not moving i dont know what happening can any one help me
It takes some time in ‘pod setup’ depends on speed of your internet connection.
I have tried all the steps for install cocoapods, But after installing pods when i open workspace on that No Scheme was available to select and run project, but i can see the pods file.
You might be missing something. Anyways, if your schemes are missing you can create a new scheme. No issue in this.
Very Nice and Simple to learn easily and Quickly
ThankYou
VeryMuch.
i was not able to see any Appdelegate .h and .m files
great if any help in.
Thank you.
AppDelegate is pre added file when you create a new project. It will not show inside cocoaPods, will only display inside your original xcode project.
i am new to cocoapods
i have to import “AFNetworking”
i am not able to import in AppDelegate default file
You can just use #import “AFNetworking.h” anywhere where you want to import. If you are unable to do this, make sure your pod is installed correctly and also you installed AFNetworking in your pod.
i can see Pods Folder with 3 targets
when i try to import #import “Af”
i can’t get the file besides i am getting AppDelegate file only in ViewController.h file
I have problem with Installing Cocoa pods. Error like UTF8 encode
I can install gems using suedo.
I want to use AFNETworking but unable to import it.
Can you help it out please?
What issue you are getting while installing pod?
I want to use AFNetworking in Swift project
had imported AFNetworking
Can you please help me how to create bridgefile to use its delegate methods
Thanks in advance.
Check this: http://blogs.triffort.com/?p=370
Simply superb tutorial.. Thanks..
Thank you very Much is a small word for you ,You saved my day after this details
thank you once again
hii… I installed cocoapod for mkmapview.
but i dont know how to run this?????can anyone help me???
There is no connection between CocoaPods and MKMapView. What you are trying to do?
i mean i want use cocoapod for map. i search on it .but i dont understand how to integrate the pod in my project…
It was well written article. Thank you! It really helped me!
Simply superb tutorial.. Thanks.. very Much
perfect thanks
Hi! I just would like to give a huge thumbs up for the wonderful information you may have here on this post. I is going to be coming back to your blog for much more soon.
Hello there,
I am getting some error while installing the cocoa pods in my system.
ERROR: While executing gem … (Errno::EPERM)
Operation not permitted – /usr/bin/xcodeproj
I have trying many different way to fix it but its still appear with an error
Please let me know how can I fix it.
Looking forward to your reply…
It¦s really a nice and useful piece of information. I am satisfied that you simply shared this useful information with us. Please stay us up to date like this. Thank you for sharing.
Thank you!
i am getting error after installing cocoapods in my application ERROR
” framework not found GTMSessionFetcher
clang: error: linker command failed with exit code 1 (use -v to see invocation) ”
how to fix this error in xcode 8.2 .
Can you post your podfile text here.
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)
i am not sure how i can fix this to build project.
You may be using wrong name of pods inside your podfile text. Please re-check.