When we work on a real project,we use many third-party libraries to make our lives easier. To use these libs,we first need to find them,download them and include them in our project.CocoaPodslets us automate all this process and also handles dependency management for us. Check out thispost on NSHipsterfor a very good intro.
CocoaPods is likeRubyGemsfrom the Ruby world andNPMfrom the Node.js world.
Using Cocoapods is really easy. You open terminal and change to your project directory (or just drag your project directory onto Terminal icon). You install CocoaPods by runningsudo gem install cocoapods
in Terminal. Then you need to create aPodfile
in your project directory. APodfile
for including very popularAFNetworkinglib will look like this:
source 'https://github.com/CocoaPods/Specs.git' platform :ios, '7.0' pod 'AFNetworking',152)!important">'~> 2.4' |
And you are all set to use CocoaPods. You runpod install
and you get an Xcode workspace file.xcworkspace
created alongside your project file. From now on you need to always open and build project using workspace file instead of project file and you will be fine.
Update 11th March 2015:This blog post is outdated now asCocoaPods 0.36,which supports Swift,has been released and you just need to dosudo gem install cocoapods
to install it. You can read complete details about what all is different in 0.36 on CocoaPods officialblog. Also don’t forget to putuse_frameworks!
before thepod
statements to support dynamic frameworks in your project.
When Swift became available,iOS programmers wanted to use their much beloved CocoaPods to integrated with already existing ObjectiveC libraries and that was not really difficult,above steps work with Swift projects as well,you just needa couple more stepsand you are done.
However soon some new and shiny libraries written in Swift came on the scene. My favorites beingAlamofireandSwiftyJSON,but CocoaPods was not yet ready for Swift libs and people had to rely on usingalternative methodsto use these libs. HoweverCocoaPods 0.36is just around the corner and it will include support for Swift Pods.
Update 25th Dec. 2014:CocoaPods decided to spread some Christmas cheer by making0.36 Beta availablefor everybody. You now just need to dosudo gem install cocoapods --pre
to install the Beta and not bother with theGemfile
andbundle install
steps given below. They are outdated and won’t work anylonger. However you may still need to use custom branch/fork in yourPodfile
as given in 3rd step and then runpod install
to begin using Swift Libs with your project.
While a public release is still some time away,you can use CocoaPods unreleased code to start integrating Swift libs in your projects right now. Just be caut@R_502_437@s,that this is not fully tested code and treat it as a stopgap measure till 0.36 comes out. Ok so let’s see what needs to be done:
- Create a file,called
Gemfile
in your project directory and just paste these lines in the file and save:
'https://rubygems.org' gem 'cocoapods', :git => 'https://github.com/CocoaPods/CocoaPods.git',115)">:branch => 'swift' 'cocoapods-core',152)!important">'https://github.com/CocoaPods/Core.git' 'xcodeproj',152)!important">'https://github.com/CocoaPods/Xcodeproj.git' 'claide',152)!important">'https://github.com/CocoaPods/CLAide.git' |
- Run command
bundle install
- Create a
Podfile
. If you want to use Alamofire and SwiftyJSON in your project,yourPodfile
1should look like this:
'https://github.com/CocoaPods/Specs.git' '8.0' 'SwiftyJSON',115)">:git => "https://github.com/orta/SwiftyJSON",115)">:branch => "podspec" 'Alamofire',152)!important">"https://github.com/mrackwitz/Alamofire.git",152)!important">"podspec" |
- Run
bundle exec pod install
2and you are done. You get the workspace file which you need to use from now on.
Now you should be able to import Alamofire and SwiftyJSON in your project and use3them.
Hope you liked this post. Connect viaTwitter&emailfor future updates. Thanks for reading.
-
Each lib should have apodspecfile to work with CocoaPods,since that’s not the case right now,we are using forks of libs which contain a podspec file. This also means that if you want to use any other Swift lib with CocoaPods,you will need to fork it and create a podspec file for it yourself and then you will need to use path of your fork in the podfile.↩
-
‘bundle exec’ ensures that you are using Swift CocoaPods version from your Gemfile. ThanksBrianofQuickfor the tip.↩