Like other dependency tools such as npm, gradle RubyGems, CocoaPods is a dependency manager (Cocoa dependency manager) for your Xcode projects.Using this CocoaPods you can add third party open source libraries to your project very easily. CocoaPods contains lots of libraries to ease our day to day application development tasks.
How to install cocoaPods?
If you have started to work with latest MacOS EI-Capitan or Sierra, you should un-install and re-install the cocoaPod like I mentioned in the below code snippet. Run below command,
sudo gem uninstall cocoapods sudo gem install cocoapods
Sometime if you don’t use sudo command like above, you might get the following error.
ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
You may ask what is
gem in the command line. It is a ruby based package Management system called Ruby gems. Don’t worry, Ruby is by default installed in all the mac os. You can run you cocoaPods installation with default Ruby that has been installed already in your pc.
Guys, don’t forget Ruby is the main player here. It does all the magics you want.
To Update your current cocoaPods, Run the below code again.
sudo gem install cocoapods
Did you get any permission issue?
If you have encountered any issues when you install the cocoaPods, try to clear the cache first.
$ sudo rm -fr ~/Library/Caches/CocoaPods/ $ sudo rm -fr ~/.cocoapods/repos/master/
Hope you have installed CocoaPods. next what? We have a dependency manager. we need to create dependant file that is managed by CocoaPods. We call it as a Podfile.
What is Podfile?
Podfile describes what are the dependency libraries we have to add to our current project. To generate the Podfile in our project, follow these steps,
Create a Xcode Project called “AlamofireTest” . I will show you how to add alamorefire framework to our project.
Close the project and goto Project path via terminal
Execute the following command in terminal,
You will see the Podfile is generated.
Open the Podfile via xcode and edit.
To add Alamorefire library to the project,
target 'AlamorefireTest' do
add following code under the above line
if you want to install specific version you can mention next to the library name like this,
pod 'Alamofire', '~> 3.0'