Need for CocoaPods
An iOS developer uses several codes which are developed by others. It can be either a set of code or in the form of libraries. It becomes difficult for the developers, if they have to implement everything from the start. A dependency management tool is essential. Without it, you might be able to, simply, add each library’s code to the project but the absence of it might have several disadvantages, for example:
- A lot of space is wasted because the library code is stored within the project.
- The lack of a central repository, where all the available libraries, can be seen, creates an issue.
- The need for updating several libraries, together, may lead to the difficulty of finding and updating one library.
- It becomes difficult to update files at a later date, if the developer needs to download and include libraries, within the project, and then store it for later use.
Overcoming Issues and Challenges
A dependency management tool is supposed to help in overcoming the issues. It fetches the library source code and resolves dependencies between the libraries. The tool, effectively, creates and maintains the right environment to build our project, by evading the trivial, yet, important issues.
Steps to Install CocoaPods to Mac
1. Open terminal and type the command.
sudo gem update –system
2. Enter the system password when requested. The terminal output should look something like this:
3. The next step is to install CocoaPods. Type this command (shown below) in Terminal:
sudo gem install cocoapods
You will get this prompt during the installing process:
Rake’s executable “rake” conflicts with /usr/bin/rake
Overwrite the executable? [y/n]
4. Enter Y to continue.
5. Finally, to complete the CocoaPods setup, enter this command in Terminal.
This process will take some time. Now, the installation of CocoaPods is complete.
If Dependency or Podfile is already Installed in the Project (For first time only)
Check for the file Podfile in the project directory. If present, then proceed following steps, else follow the steps in Install First Dependency (given below this topic).
1. Open Terminal and type the command to navigate to the directory containing the project:
(This is just an example, give your project path to the folder containing xcodeproject file)
2. Enter the command in the Terminal to “install” the dependencies.
3. Similar output will be displayed:
Using ECSlidingViewController (1.3.3)
Generating Pods project
Integrating client project
4. You will notice something like this:
[!] From now on use ‘MyProject.xcworkspace’
(This is an example)
5. After this step, you can start working in the workspace that is created.
Installing First Dependency (Cocoapods to the XCode project for the first time)
1. Open Terminal and navigate to the project directory, by using the cd command:
(This is an example, give your project path to the folder containing xcodeproject file)
2. Next, enter this command:
3. This will create a default Podfile for the project. The Podfile is where we define the dependencies for the project.
4. To open Podfile, using Xcode for editing, type this command:
open –a Xcode Podfile
5. The default Podfile should looks like:
# platform :ios, “6.0”
target “MyProject” do
(This is an example)
6. Replace # platform :ios, “6.0” with the following
# platform :ios, “7.0”
Many libraries have minimum iOS version requirements. If this is omitted, CocoaPods assumes a default target version.
7. Now, add the dependency that is going to be used in the project. Enter the required pod in the Podfile, right after target “MyProject” do:
pod ‘ECSlidingViewController’, ‘~> 1.3’
(This is just an example)
8. Save and close the pod file.
9. Follow the steps in if Dependency or Podfile already installed in the Project (as mentioned above).
Adding More Pods
If more dependencies need to be added to the project, rather than opening the PodFile using the command line, we can open it from the Pods target in the workspace.
1. Select Podfile, add the pods you need, under the existing pod.
pod ‘AFNetworking’, ‘2.2.1’
(This is an example)
2. Save the file, and install the dependencies (using pod install in Terminal, as before).
Know More About CocoaPods
This article provides some pointers for beginners, to start using CocoaPods. To obtain more knowledge about CocoaPods, go through CocoaPods Guides, which includes how to create your own pod spec and submit it to the official repository. And if you need information on whether CocoaPods supports a specific library, you could visit the CocoaPods website. The website provides useful information.
Rohit Murali Warrier
iOS Developer, RapidValue Solutions