10 Steps for Cocoapods
CocoaPods is an application level dependency manager for the Objective-C, Swift and any other languages that run on the Objective-C runtime, such as RubyMotion, that provides a standard format for managing external libraries.Love it or hate it, you have to use it. Cocoapods exists since the old Objective-C days and works with Swift as well. This is mostly used dependency manage so far for the iOS project which is the de facto standard tool.
CocoaPods is built with Ruby, if you haven’t used Ruby yet just know … it’s as easy as it looks. I was scared by the simplicity coming from Objective-C but the CocoaPods library has pretty good error handling and troubleshooting.
Why Cocoapods not other dependency manage?
- CocoaPods is easy to set up.
- CocoaPods automates the entire process of building and linking the dependencies to targets.
- The CocoaPods community has done an impressive job of fixing Apple’s shortcomings.
- CocoaPods supports libraries having subspec.
- Lots of contributors with a well-grown community.
- Works well for small projects with less code to get something working very quickly.
How to use Cocoapods in my app?
CocoaPods is built with Ruby and is installable with the default Ruby available on macOS. We recommend you use the default ruby.
1- Install the Cocoapods in your machine
$ sudo gem install cocoapods$ pod setup --verbose
2- Check whether Cocoapods is install in your machine or not?
$ pod --version
3- Go to your project directory where .xcodeproj is present
By using commands in terminal or just drag the folder of project into your terminal it automatically open project directory.
4- Intalise the Cocoapods by using command pod init
After using `init pod` one aditional file is added in you folder of name Podfile.
5-By double click open the podfile
All the lines which is start from # is commented, In Ruby # is used to comment the single line.
6- Search for available pods on the website: https://cocoapods.org/ — We will use CLTypingLabel in this example.
Search the pods which you want to use in your app in cocoapods main file.
8- Remove the # from ->platform :ios, ‘9.0’
9- Install Pod using command pod install
When you run pod install command it install all pods which you write in pod file. And after you install the required commands open the project directory you get 2 new file.
First is Podfile.lock -> Every time the pod install command is run — and downloads and install new pods — it writes the version it has installed, for each pods, in the Podfile.lock file.
- CocoaPods will build all the libraries mentioned in the Podfile for that target, there might be some cases where you don’t want to build some libraries all the time but CocoaPods won’t have that option.
Second is .xcworkspace -> Xcode workspace files can be used as a wrapper or container for an Xcode .XCODEPROJ project.
10- Now use your library by using .xcworkspace which have white white background color.
How to Remove CocoaPods
Once you adopt CocoaPods, there is no clean way to de-integrate it from your project. CocoaPods has made lots of changes all over your Xcode project, build settings, and directory structure. As you have seen, integrating CocoaPods takes a minute, but removing it from the project is yak shaving. CocoaPods has the pod deintegrate
and pod clean
commands, but still, we need to make sure of what it has done. However, there are some manual actions to get rid of CocoaPods from iOS projects completely.
- Delete the standalone files (
Podfile
,Podfile.lock
, and yourPods
directory). - Delete the generated
xcworkspace
. - Open your
xcodeproj
file and delete the references toPods.xcconfig
andlibPods.a
(in theFrameworks
group). - Under your
Build Phases
, delete theCopy Pods Resources
,Embed Pods Frameworks
, andCheck PodsManifest.lock
phases. - This may seem obvious, but you’ll need to integrate the 3rd party libraries some other way or remove references to them from your code.
Conclusion
CocoaPods is a tool that makes managing your project much simpler. It can save you a lot of effort and time when dealing with dependencies in your project as it makes adding, removing and updating libraries that much easier. For more on using and troubleshooting CocoaPods, check out the CocoaPods guides.