Adding Your First Pod
What is a pod?
Remember those English pair matching tasks? Fish is to water like bird is to tree, or something like that? That’s how I think about pods. When I first started learning Swift, I had no idea what a pod was. Now I think that a pod is to iOS what a gem is to Ruby or a macro is to Excel.
Pods are packaged up bits of code that you import into your Swift file. Once imported, you can use that code in your project. Often a pod will make a task or a process more logical, more readable or perhaps faster to develop.
For example, Swift has methods you can use to parse json “files”. However people use the SwiftyJSON pod to parse json files, because the methods in SwiftyJSON are more readable. You end up with roughly the same number of lines of code, but it’s faster to write and more readable to debug.
Adding a pod to your Xcode project
When I wanted to add my first pod, I was pretty confused. It was SwiftyJSON, as I was trying to access an API. The GitHub documentation for installing the pod seemed very straight forward:
There is also the helpful folk from CocoaPods themselves, who have this lovely guide. However there was just one simple thing that stumped me. Where was my Podfile?
I’m finding this to be a very common problem. Lots of basic “how to” guides state something that must be really obvious. Too obvious to explain further. Unfortunately it’s really not that obvious to me!
For others that are equally confused, here’s the simple answer! It’s a file, just like any other file. Once you have installed CocoaPods on your computer, you are then ready to follow the basic steps listed here:
Now for that pesky little statement “Open your Podfile”. I’m sure there are lots of different ways to open it. Opening a text editor from Terminal is still on my “to learn” list. Right now, I just rejoice in the fact I know where my Podfile is in Finder. So I do it the old-fashioned way, by double clicking on it and opening it in text editor.
Side note: I learnt you can also type nano podfile to open it in Terminal.
Once you open it, then you can follow the instructions from SwiftyJson. If you are performing tests, you have the option to include the pods into the test files. I am using Specs for my tests. Below is an example of what a podfile looks like. Very simple!
platform :ios, ’10.0’
target 'MyProjectName' do
// This section is for including pods in your tests.
// I'm not sure if it's needed, but that's what I've done.
target 'Specs' do
Once you have saved this podfile, go to Terminal and type pod install.
Notice that there are two files for your project: .xcodeproj and .xcworkspace. Both of these files will open in Xcode and will both seemingly run your project. However only .xcworkspace contains the pods. So going forward, you will always need to use .xcworkspace.
Adding pods for testing purposes
Update: I touched on this at the end of the last section. However at that time, I hadn’t really interacted much with setting up tests or TDD. Now I have a better understanding, so I’ve added this section.
The two common pods for testing are Quick and Nimble. They go together like SwiftyJSON and Alamofire.
However tests live in a separate ‘target’ to your base project. Let’s go through the setup together.
Note there’s only one target at the moment, ReviewsFetcher. This is my project. I need to add another target for unit tests and UI tests. I do this by clicking on the plus button.
I clicked next and let Xcode pre-populate everything for me. Add another target for iOS UI Testing bundle.
Now you should have a total of three targets in your Xcode project.
Install your pods
At this stage you’re able to update your Podfile to install Quick and Nimble. Here’s what my Podfile looks like.
Note I haven’t installed SwiftyJSON and Alamofire yet.
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
# Necessary for using Swift in CocoaPods
target 'ReviewsFetcherTests' do
target 'ReviewsFetcherUITests' do
target 'ReviewsFetcher' do
Using your pods
Commit your changes. This will be a big commit. I had 147 changes after this installation.
Close your project and open your project workspace. Remember you’ll need to use the workspace going forward, as that’s where the pods live.
Here is what your project workspace should look like:
Running tests in your project
Click on the project and “edit schemes”.
Click on the “test” option. On the bottom, click on the + sign.
Add the test to the ReviewsFetcher target.
Repeat for the UITests. Now your test screen looks like this:
Now if you need to run tests, you can just press command U to run all the tests. Remember the target membership of a test file is just the test target, not the main project target.