Dependencies

How To Manage Dependencies With Cocoapods

Dependencies allow us to use other people’s code in our own projects. A good way to add dependencies to your own Xcode project is to use a dependency manager. In this guide, we will be using Cocoapods which makes it easy to add, update and manage third-party dependencies for your app.

Installing Cocoapods

To get started with Cocoapods, you first need to install it. This can be done using Ruby Gems. Open up the terminal and enter the following command.

sudo gem install cocoapods

If prompted, enter you password and this should go ahead and install Cocoapods to your machine. To make sure that it has installed correctly, you can enter the following command to check the version number.

pod --version

Creating a New Xcode Project

If you’re starting with a brand new app, open Xcode and create a new Single View App. I’m going to call mine DependencyTests but feel free to name yours whatever you like. I’m not going to use tests in my project so I’m leaving that unchecked. Make sure to save the project to an easy location such as the Desktop.

Creating a Podfile

Cocoapods revolves around one central Podfile. This file contains a list of all the dependencies for your Xcode project. To create the file, we first need to close down our Xcode project and head back to terminal. Navigate to your project’s directory.

cd Path/To/Project/DependencyTests

Once there, enter the following command.

pod init

This creates a new blank Podfile in the root of your project directory. You can navigate to this Podfile in finder to open it or you can type the following command to open it with Xcode.

open Podfile -a Xcode

Xcode should open and display the contents of the Podfile which contains the following information by default.

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'DependencyTests' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for DependencyTests
end

We’re going to uncomment line 2 and remove all the other comments. Your Podfile should now look like this.

platform :ios, '9.0'

target 'DependencyTests' do
  use_frameworks!

end

Adding a Dependency

Adding dependencies is really easy to do. We’re going to add the SnapKit dependency to our project. Looking at the Github readme, its clear we just have to enter one line of code to our Podfile. Add this just under the use_frameworks! line.

pod 'SnapKit'

This tells Cocoapods to add SnapKit to your app. You can add as many pods as you like here, just make sure each one is on a new line and they start with the pod keyword. Note: you can add a version number to each pod if you would like.

We can now save and close the Podfile and back in terminal run one final command, making sure you’re still in the same directory as the Podfile.

pod install

You should see an output similar to this.

pod install
Analyzing dependencies
Downloading dependencies
Installing SnapKit (5.0.1)
Generating Pods project
Integrating client project

Using the Dependency

To use the dependency, head to your project directory in finder and open the white DependencyTests.xcworkspace file. Make sure to access your project through this workspace from now on otherwise you won’t be able to use the installed dependencies.

Finally we can check to see if SnapKit has been added correctly by heading to the ViewController.swift file and importing SnapKit.

import SnapKit

Upon building the project, there should be no errors and you can use the dependency.

Adding, Removing and Updating Dependencies

If you ever need to make any changes to your project’s dependencies, just open up the Podfile to make any changes such as adding a new pod or changing a version number. Save and close the Podfile and run the following command

pod update

Conclusion

You’re now hopefully familiar with how to use Cocoapods to manage dependencies in your Xcode project. If you’re interested in which dependencies can be added through Cocoapods, you can head to the Cocoapods Website. Thanks for reading!