Lesson 6 - Don't reinvent the wheel, use CocoaPods
In the previous lesson, Introduction to the important TableView component, we learned to use
Have you been programming in a different language or you just think it isn't
necessary to invent the wheel over and over again? You're in the right place
today. We're going to have a look at how to avoid programming the same
The individual packages are called pods and allow us to simply reuse a functionality other person already programmed. In this lesson, we're going to learn how to get the pods into our project and introduce a few you should know about.
To be able to use the CocoaPods power, you have to install CocoaPods on your
Mac first (there are two approaches). Then you prepare your project for use with
CocoaPods. You specify which packages you want and then you can just use
import. Don't be afraid, you'll see it's worth it in the end of
Are you friends with the terminal? It's one of the ways to install CocoaPods on your Mac and then add it to your project. There is also a simple CocoaPods application that does the same. If you don't want to write commands in the terminal, you can just skip this part of the tutorial.
Open the terminal and start by installing the CocoaPods itself:
sudo gem install cocoapods
The installation may take a while.
If you decided to install CocoaPods using the application, download the official CocoaPods application from its website. That's all for now.
How pods work
Now let's explain, how pods work in a project. Until now, we've had one project per app. Pods have its own project, so CocoaPods will create a Workspace from your project first. Simply put, that means you will have more projects "under one roof". We don't have to think about it much.
The second important part of CocoaPods is the
Podfile file in
every project. You can specify in it what pods you want to use and CocoaPods
will take care of the rest.
Now, let's have a look at how to prepare our projects for pods. In the
terminal, open the folder of your project, where you want to add the pods. You
should be in the very root folder, which means you should see a file with the
.xcodeproj extension representing your Xcode project. Close
Now initialize CocoaPods by this command:
Again, just wait and your project is ready for pods.
Click "File" in the application menu and choose "New Podfile from Xcode
Project". Now we have to navigate to the project folder and select the file with
.xcodeproj extension. The CocoaPods application will create and
You've probably already noticed the changes. Better said a change
which is the
Podfile file added to the root folder of our project.
Right here, we can set what pods we want to use.
You can edit the
Podfile in any editor you want. If you've
decided to use the application, it makes sense to edit it right there. If you've
Podfile via the application, the file should already be
opened. If you took the terminal approach, you can open it in anything, even
Xcode will do.
Podfile is basically empty now, it only contains the
information about for which project it was created:
project 'Cocoapods_ICTsocial.xcodeproj' # Uncomment the next line to define a global platform for your project # platform :ios, '9.0' target 'Cocoapods_ICTsocial' do # Comment the next line if you're not using Swift and don't want to use dynamic frameworks use_frameworks! # Pods for Cocoapods_ICTsocial end
Let's do what the comment is telling us to do and uncomment the line with the
global platform definition. That means deleting the
# which is used
platform :ios, '9.0'
We can show the use of pods on a small, but very useful pod named Chameleon.
It's a framework for iOS colors. You can find the instructions how to install it
via CocoaPods on the project's GitHub page.
Following those, let's add Chameleon as one of the pods under the
# Pods for Cocoapods_ICTsocial comment in our file:
pod 'ChameleonFramework/Swift', :git => 'https://github.com/ViccAlexander/Chameleon.git', :branch => 'wip/swift4'
The command is a bit more complicated to make the framework work with the
current version of Xcode and Swift 4. Usually the
pod keyword and
the name of the project will do. Let's have a look at how the whole file looks
project 'Cocoapods_ICTsocial.xcodeproj' # Uncomment the next line to define a global platform for your project platform :ios, '9.0' target 'Cocoapods_ICTsocial' do # Comment the next line if you're not using Swift and don't want to use dynamic frameworks use_frameworks! # Pods for Cocoapods_ICTsocial pod 'ChameleonFramework/Swift', :git => 'https://github.com/ViccAlexander/Chameleon.git', :branch => 'wip/swift4' end
If you've used CocoaPods via terminal, save the
Podfile now and
run this command:
If you've used the CocoaPods application, just click the "Install" button in the top right corner and wait a while.
The project with Chameleon
The project looks more interesting now. There's the
where our pods are stored and also the very important file with the
.xcworkspace extension. From now on, we'll use this file to open
the project, because the pods have its own projects and if we tried to open it
through the original
.xcodeproj, it wouldn't work.
So let's open the project via
.xcworkspace and try to use
Chameleon right away.
We'll move to the
ViewController.swift and add an
import for the freshly installed pod:
We'll try to build the project (Cmd + B) to make sure everything is okay. Using the Chameleon framework doesn't do much in an empty project, so let's make sure it works.
Chameleon provides, for example, much prettier colors than the system ones.
You can find a list of them on the project page and access them using the
UIColor system class which now has more colors available. Xcode
will show us e.g. the available pastel "flat" colors:
It's worth to consider using the framework just because of the colors. The app will look a bit better with little to no effort.
Chameleon can also help us with gradients. It can generate colors from images or set a nice contrast color according to a given background. That's useful when we have dynamic colors and always want the text to be readable.
This can be done just by using a single method, which returns a contrast
UIColor for the given background. You can also choose whether a
flat color should be returned.
ContrastColorOf(UIColor.flatBlue, returnFlat: true)
In one of the further lessons, we'll use CocoaPods when trying to get data
from the Internet (in the JSON format) and to process them. Both can be done in
pure Swift, but the
make things much easier. We can say that these two have become the unofficial
standard for apps that download data from the Internet and process the JSON
In the next lesson, When a single screen isn't enough - Navigation in iOS, we'll have a look at navigation and start writing the promised TODO app
No one has commented yet - be the first!