It's unlikely that you'll ever create an iOS app where you don't need to depend on some external libraries (and there's so many to choose from, just check out any installment ofThis Week in Mobile). There are a few options for managing these dependencies. There's the official Swift Package Manager , but that's in its early stages and targeted more toward Swift 3. Then you're down to a choice of two: Carthage or CocoaPods . Typically people use Carthage if they want to have more control over how the packages integrate. CocoaPods, the more established of the two, will create and update the Xcode workspace to integrate the frameworks. This five-minute tutorial will show you how to integrate CocoaPods into your projects
If you haven't already installed CocoaPods, it's simple. Just open up the terminal and use the gem package manager to install it. I know that seems weird, but it has to come from somewhere!
sudo gem install cocoapods
Once that completes you'll need to clone the master repo to your machine using the following command:
That will take a while to complete, but when it does, you are good to go.
Integrating CocoaPods into Your Project
I'll assume you've already got a project started — you'll need that first! To use pods, you are going to need to add a Podfile to the project, and it couldn't be easier. First, go to the folder that contains the project in your terminal and type the following to initialize a Podfile:
And now you'll have a Podfile in that same directory. This is where you'll add your dependencies.
If you open the Podfile you'll see something like this:
# Uncomment this line to define a global platform for your project # platform :ios, '9.0' target 'MyProject' do # Comment this line if you're not using Swift and don't want to use dynamic frameworks use_frameworks! # Pods for MyProject end
It's pretty self-explanatory; if you're using Swift, there's nothing more to be done. However, for those on Objective-C make sure to comment out that use_frameworks! declaration.
Adding a Third-Party Dependency
Let's add in a third-party library into our project. For this example, we'll add RxSwift . Usually, the project page on GitHub will have details on how to integrate this as a pod, as is the case here . As well as needing to include RxSwift, we need RxCocoa:
# Uncomment this line to define a global platform for your project # platform :ios, '9.0' target 'MyProject' do # Comment this line if you're not using Swift and don't want to use dynamic frameworks use_frameworks! pod 'RxSwift', '~> 2.0' pod 'RxCocoa', '~> 2.0' # Pods for MyProject end
As well as declaring the name of the pod, you can include an optional version. It's good practice to do this so that everyone in your team is on the same version. Assuming that being on the latest is OK is not a good strategy!
Finally, we need to have these packages installed, which is done by typing:
You'll be prompted to close down any Xcode sessions related to this project, and to use the .xcworkspace from now on.
Adding Dependencies Later
You can keep on editing the Podfile as required; each time you make an update, you can use the following command to get your workspace in sync.
Here's your cheatsheet to getting started with CocoaPods:
Install CocoaPods on your machine
(first time only)
|Initialize a Podfile in your project directory||
|Add dependencies to your Podfile||Edit the Podfile in a text editor/Xcode|
|Install the dependencies||
|Add additional dependencies||
Update the Podfile