Get up to 80 % extra points for free! More info:

Lesson 6 - Don't reinvent the wheel, use CocoaPods

In the previous lesson, Introduction to the important TableView component, we learned to use TableView.

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 things.

Swift doesn't officially support packages, unlike e.g. NuGet for .NET projects, but that's no big problem. There are a lot of unofficial solutions and probably the best is CocoaPods.


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 this lesson.

Terminal approach

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 Xcode.

Developing iOS Applications in Swift

Now initialize CocoaPods by this command:

pod init

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 the .xcodeproj extension. The CocoaPods application will create and open a Podfile.

Folder for CocoaPods initialization - Developing iOS Applications in Swift

Editing Podfiles

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.

The created Podfile - Developing iOS Applications in Swift

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 created the 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 opened in Xcode - Developing iOS Applications in Swift

The 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

  # Pods for Cocoapods_ICTsocial


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 for comments:

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 => '', :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 now:

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

  # Pods for Cocoapods_ICTsocial
  pod 'ChameleonFramework/Swift', :git => '', :branch => 'wip/swift4'



If you've used CocoaPods via terminal, save the Podfile now and run this command:

pod install


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 Pods/ folder 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.

Xcode project with the installed cocoapods - Developing iOS Applications in Swift

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:

import ChameleonFramework

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:

Chameleon Framework flat colors for Swift - Developing iOS Applications in Swift

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 AlamoFire and SwiftyJSON libraries 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 format.

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 :)


Previous article
Introduction to the important TableView component
All articles in this section
Developing iOS Applications in Swift
Skip article
(not recommended)
When a single screen isn't enough - Navigation in iOS
Article has been written for you by Filip Němeček
User rating:
No one has rated this quite yet, be the first one!