CocoaPods is one of the famous dependency manager used by most iOS Developers ranging from Swift to Objective-C. A It is definitely a plus point if you know how to use CocoaPods as it will make your life easier in which you import a set of framework written by other developers and write shorter codes.
CocoaPods save you time in implementing third party framework. In this instance, when you use CocoaPods, it will import everything for you instead of you copying and pasting the file manually.
CocoaPods has over 39 thousand libraries and is used in over 2.7 million apps. If you check the infographic that I made of apps on App store, you clearly know that over 85% apps on App Store uses CocoaPods. With such popularity, we believe it brings more good than harm to us in using it.
SETTING UP COCOAPODS ON YOUR MAC
In order to install CocoaPods, you have to open Terminal application and type the following code.
sudo gem install cocoapods
Your screen should like this indicating successful installation on your Mac. If it asks for password, it should be the password that you have set for your account.
IMPLEMENTING COCOAPODS ON XCODE PROJECT
This is the exciting part in which we could use a third party framework and apply it on our project. I have used it a number of times and it has definitely helped me a lot. Go to CocoaPods and start looking for the third party framework that you would like to implement.
I typed TEXT hoping to find something that I could implement. Here, we will pick TextFieldEffects. Click on that and more information will be revealed.
A quick tips is to always know that whenever you clicked on a pods, is to always check the Swift version of it. You want to make sure that it is the latest Swift version. If it’s the older version Swift, it will most likely not work very well on your project.
Now scroll down and click on Installation Guide on the left side bar and it should pop up some instructions.
In order to do this, we first have to find the Xcode project that we would like to implement this framework using Terminal. Use terminal to navigate to your project. My project is saved on the Desktop, so I would type the following code on my terminal:
cd meaning choose directory – Here, I am telling the terminal that I would like to navigate to Desktop in which there is a folder named CocoaPods Test which is my Xcode Project.
If your Xcode project is saved somewhere else, for instance your user, you could replace Desktop to your user.
Now that we are in the Xcode project, we will have to create Podfile by typing the following command:
You will notice that on your folder, there is an additional file called Podfile that was generated through the previous command. This Podfile is where the work to import the framework can be done.
I am using TextMate, my favorite editor for Mac. There are many different editor that you can use like TextWrangler and so on. But for now, we will use TextMate. If you do not have it, feel free to download it. To access the Podfile with TextMate,
The file will then be opened through TextMate. The content of TextMate will look like this:
# Uncomment the next line to define a global platform for your project # platform :ios, '9.0' target 'CocoaPods Test' do # Comment the next line if you're not using Swift and don't want to use dynamic frameworks use_frameworks! # Pods for CocoaPods Test end
Following the installation guide, we will add a line of code into the Podfile. Notice that I have add pod ‘TextFieldEffects’, ‘-> 1.4’ following the instruction of installation guide. Make sure to save the file.
# Uncomment the next line to define a global platform for your project # platform :ios, '9.0' target 'CocoaPods Test' do # Comment the next line if you're not using Swift and don't want to use dynamic frameworks use_frameworks! pod 'TextFieldEffects', '~> 1.4' # Pods for CocoaPods Test end
Now, we just need to install it by typing the following command
The result should look like this
You will now notice that there is additional files on your project. From now on, you will have to open CocoaPodsTest.xcworkspace instead of CocoaPodsText.xcodeproj. CocoaPodsTest.xcworkspace is where the framework has been implement.
Your Xcode project should now look neat and clean. On the left navigator, you’ll see the framework imported into the project. Always remember to build your project first by clicking Command + B to know if the framework works for you. If it does, you’ll see the positive message. Then you can go ahead and import the framework.