Dependency Management with Cocoapods for iOS
Every iOS Developer will come across with Cocoapods during their development journey. You have probably seems many of them as they are of great use. For example: they can interface with Core Data, help with animations or transitions, help parse JSON responses, and many other processes that developers require frequently. Basically, you incorporate other people’s tools as your own for your projects.
CocoaPods is an industry standard Dependency Manager, in other words, they are 3rd parties frameworks and libraries that will give you easier functionalities and tools, so you can focus on what’s really important — creating great apps! You can check thousands of Cocoapods in its website: https://cocoapods.org/. Cocoapods was originally launched in 2011 to support de dependency management in Objective-C. Then, in 2015, it started supporting the Swift Language.
Cocoapods is designed to use a text file called Podfile to deliver dependencies to a project. It downloads the source code and dependencies for the libraries you need for your project and links them to your XCode workspace. This brings automation that helps you create and manage dependencies easily so you can spend more time in the creation of your application as a whole than creating these tools.
To use Cocoapods you basically need to do the following:
- Install Cocoapods in your machine: The following steps are done only once in the Terminal. Once in the Terminal type the commands I have in italics shown below:
a) sudo gem install cocoapods. Wait for the gems to be installed.
b) pod setup — verbose . This this copy the Cocoapods specifications files in your local machine.
This process should install Cocoapods in your machine.Your computer is ready to handle Cocoapods dependencies now. This step should not be repeated again.
2. Learn what is a Podfile and how to use it
a) Create a Podilfe. A podfile is a text file with a set of instructions on how to use a given Cocoapod. It provides key information about the libraries to be used. To create a Podfile, use Terminal in your machine and navigate to the folder that contains your project. Once there you need to type the following:
pod init
open -a Xcode Podfile
Once you create the Podfile and you opened, the next step is to remove the comment for ios platform support and then add the pod you want to use with its latest update. It is very possible that you will need to update that particular Podfile. If so, go to the source Cocoapod file for further instructions. After these steps, save the changes in the Podfile and go back to Terminal. Once there type:
pod install
This will install that particular pod you just wrote in your Podfile. You will need to do this step for every pod you want to install in your Podfile project.This will copy your project directory into a workspace. From then on, you need to do all your work in the workspace generated for your project because this is where your pods will be. Do not use the original project file anymore!
Cocoapods is not the only dependency manager for iOS development. There is also Taylor, Carthage and many others. Cocoapods, as many 3rd parties libraries can also have its downsides. For example, since your project now depends on a 3rd party library, if that particular library breaks or is not updated, your project will follow the same path.
In a future post, I will do a tutorial on how to use a simple Cocopods in a project and how to update it.
For more information about Cocoapods, you can visit the following links:
