Friday, August 30, 2019

How To: Use Cocoapods for Your XCode Projects

Hi guys. Been a while eh? As for me, been busy with updating my apps. I just updated one of my kids game Eggs Surprise With Friends. Improved on the graphics and gameplay based on user feedback back in 2016 HAHAH! First, an intro.

Human beings rely on blind beliefs to have a large-scale cooperation. We all believe in something that is not there physically. We believe in God (some of us not all of course). God is nowhere to be seen, cannot be measured, cannot be heard, cannot be touched. We believe in our country. Yet, a country is merely a make belief concept, and exists only in maps. We believe in the company we work in. But the company does not exist other than on papers. Our collective belief that the company exists, make us feel we belong to it, and therefore we can together work towards a common goal - be it increasing the company's profits for employees' benefits, or prospering a nation, or mass producing products. Belief in the unseen is important and needed for humanity, unless when it starts to oppress others.

How's that for an intro? :D

Back to topic: COCOAPODS.

Wow I never saw an actual cocoa pod before.. now I know how it looks like! What a great thing google is. What is CocoaPods? Why is it called Cocoa? Cocoa is basically Programming API for Mac OSX. Basically it is a set of functions, classes, frameworks that enables you to create a Mac Applications/Softwares. And iOS imports some of those as well. Back then, I was so occupied in creating my own controls, customizing UI is one of the fun thing to do! I still love to customize UIs. Here are some of the controls that I made in the past - ranging from noobiest, to a fairly intermediate level where I am now.

CocoaPods are basically small components that are made by other developers worldwide, using Cocoa. When I learnt about cocoapods, I couldn't really understand what it is. A simple way to explain is CocoaPods is like more options to your UIViews, UISwitch, and UITableViews. You can contribute to it by making your own controls and user interface objects. Or you can simply use them.

For example, lets say you are making a drawing app. You need to code the drawing part, how the touches translates to lines and curves. And then you also need a way to pick a pen color. Here is where you have that option - do you create your own color picker?? Or, simply import a color picker object made by others from CocoaPods library? There are plenty of CocoaPods color pickers. You can browse them here:

But how do we add any of these cocoapods to our XCode project? Here is where I got lost and gave up on CocoaPods lol. No don't give up. I will teach you how.

First step is to INSTALL COCOAPODS into your Mac. This is easy really. You are going to use some terminal commands but don't worry it is really straight forward. I actually outlined it in the StackOverflow answer here, but I am going to outline it again here in this blogpost in case somebody edited that answer.

[ 1 ] Open terminal and type:

sudo gem install cocoapods
Gem will get installed in Ruby inside System library. Or try on 10.11 Mac OSX El Capitan, type:
sudo gem install -n /usr/local/bin cocoapods
If there is an error "activesupport requires Ruby version >= 2.xx", then install latest activesupport first by typing in terminal.
sudo gem install activesupport -v 4.2.6
[ 2 ] After installation, there will be a lot of messages, read them and if no error found, it means cocoapods installation is done. Next, you need to setup the cocoapods master repo. Type in terminal:
pod setup
And wait it will download the master repo. The size is very big (370.0MB at Dec 2016). So it can be a while. You can track of the download by opening Activity and goto Network tab and search for git-remote-https. Alternatively you can try adding verbose to the command like so:
pod setup --verbose
[ 3 ] Once done it will output "Setup Complete", and you can create your XCode project and save it.
[ 4 ] Then in terminal cd to "your XCode project root directory" (where your .xcodeproj file resides) and type:
pod init
[ 5 ] Then open your project's podfile by typing in terminal (or open using Atom/TextEdit/Notepad++ etc):
open -a Xcode Podfile
[ 6 ] Your Podfile will get open in text mode. Initially there will be some default commands in there. Here is where you add your project's dependencies. For example, in the podfile, type
pod 'AFNetworking', '0.9.1'
(this line is an example of adding the AFNetworking library to your project).
Other tips:
Uncomment platform :ios, '9.0' Uncomment user_frameworks! if you're using Swift
When you are done editing the podfile, save it and close Xcode.
[ 7 ] Then install pods into your project by typing in terminal:
pod install
Depending how many libraries you added to your podfile for your project, the time to complete this varies. Once completed, there will be a message that says
"Pod installation complete! There are X dependencies from the Podfile and X total pods installed."
Now close your Xcode project. Then locate and open the .xcworkspace Xcode project file and start coding. (You should no longer open the xcodeproj file)

Step [6] is where you import any of the CocoaPods that you like to use. The command line to import is generally "pod [podname], [version]". Lets choose a cocoapod and install it. I found a iOS-color-wheel pod, so I click on it and its own page opens up. Then on the left there is a Installation Guide button, click on that and there will be a popup with the command line in the middle (see arrow).

Copy this and paste in your "Podfile" file on your text editor.

WARNING! Do take note what language that the pod was written on. Some are Swifts some are ObjCs. So far I used ObjC only so I am not sure what happens if you use Swift's pod in an ObjC project. My guess is it won't work as expected.

After you added the command in your Podfile, save it and return to the command line where your Podfile resides and do step [7] above.

If later in your development, you want to add more pods to your project, then you simple open your Podfile again, copy paste the pod command lines and run the "pod install" in your terminal. And then you can use those pods normally by importing their headers, like any other normal libraries/frameworks.

Here is a sample of one of my apps' Podfile that uses multiple pods:

Here you can see I even have 2 targets (Pro and Lite app) and I only use Admob ads pod in one of the target (Lite version has ads). So everytime you update an app, you can also update your pods' version (if any, depending on their developers). Just run "pod update".

One last thing, is to remember to open your XCode project, by opening the newly created .xcworkspace file instead of the xcodeproj file. That will load the correct files for CocoaPods to work properly with your project.

Cocoapod has grown big. A lot of developers contributed to this library. As people say, why reinvent the wheel? Well, in some case, we do need to reinvent the wheel, if there aren't any good library out there to do a particular thing, or if you think of something better, do create it and add to the pod. I am planning to add my controls to the pod as well, but maybe later after I am able to code in Swift proficiently.

That's all for now.... cya later

No comments:

Post a Comment