If you are a iOS developer and haven't use pod file / CocoaPod yet, you can understand the pain how you need to integrate frameworks manually. Yes, in this post I am talking about dependency management. All developers know very well that stand alone project requires some 3rd Party framework for animations, monetisation, analysis, or to create app more attractive and eye catching. Earlier, you need to add all dependency manually with drag and drop framework files into your project, this work is very boring, time taking and sometimes get integration error as well. To overcome this issues CocoaPod come into picture. So, If you are unaware about CocoaPod and dependency management tool then this small post will help you to understand and trust me after reading this post you will stop using manual process and start using it.
What is CocoaPods?
It is a dependency manager for Objective C and Swift projects that manages all framework into your project and integrate all required frameworks automatically. So, instead of import file with drag and drop CocoaPods take care everything.
Let's take an example, If you are developing a security application and you want to integrate cryptography(encryption / decryption) then what will you do? You will simply google it, find some useful framework / repository which fulfil your goal and drag and drop those files into your project.. right? We will integrate same repository/framework with CocoaPods in this post. I have found a good Cryptography repository CryptoSwift which we are going to import in our project.
How to install CocoaPods in my mac?
First, you can check whether your system already has CocoaPods or not. For that, open terminal and enter below command into root directory.
If you get response like below image then your system already has installed CocoaPods but if not, you need to install it only for once.
To install, enter below command:
sudo gem install cocoapods
It may prompt to enter password, enter your system password and hit enter. It will take some time to download and display below message in your terminal screen.
Done ! Now you can use CocoaPods into your project. Isn't it easy like cup of tea?
Use CocoaPod into Main Project
Once CocoaPod installed into system, create test project and name it CocoaPodTest. Close the xCode and open terminal again. Go to your project directory path. For example I have created project on desktop. Just hit below command in terminal:
It is because pod file which we are going to create is Project specific and we need to create it into our project root directory. Above command will redirect terminal to CocoaPodTest directory. So, now whatever command will fire in terminal will apply to this directory only. I hope it will clear to you.
We need to create podfile in project root directory. It will track all pods which you want to install. When you ask CocoaPod to install/update podfile this file will execute. To do so type below commands step by step.
1. To create podfile:
2. Open Podfile:
IMPORTANT: Podfile is now generate with below lines of code: (You can open it into text editor)
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'CocoaPodTest' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
# Pods for CocoaPodTest
target 'CocoaPodTestTests' do
# Pods for testing
target 'CocoaPodTestUITests' do
# Pods for testing
3. As described above, we want to install CryptoSwift in our project. You will find it's pod name from repository link. Enter pod just below use_frameworks! line. Save the file and close the editor.
4. Type below command to install CryptoSwift pod:
Done ! Your terminal will show as shown below:
And your Podfile looks like,
Now, you have noticed that CocoaPodTest.xcworkspace file is created automatically into your project root directory. Now, on words you have to open this file instead of using CocoaPodTest.xcodeporj. It has installed given pods into project and ready to use. No bridge file is required or no more drag & drops to integrate framework.
You can import it with simply adding import CryptoSwift code in swift file. It is as simple as I told you initially !!!
Add another Pod into existed Podfile
Most of the cases we need to integrate Podfile/ 3rd party frameworks as per our requirement and it is not predefined before project creates. On the middle of the project if you want to add another pod then it very simple. If you have xCode open with the same project,
1. Just click Podfile and open it.
2. Add pod name as described above Step No 3 and save the file.
3. Open Terminal and go to project root directory and type below command:
4. This command will install missing pods. It will not reinstall pods which are already exist into project.
Xcode Project CocoaPodTest.xcworkspace will looks like:
This way you can update project podfile, I hope this post will clear all concept of CocoaPods and you will start using it.... Happy coding... :)