CocoaPods is a great dependency manager for iOS projects. It will help you to download additional libraries and update them later without much effort. If you haven’t heard of it yet, read more about it on Cocoapods website.
There is also an additional very popular dependency manager for iOS: Carthage. I’m not going to install it here, but feel free to try it by yourself.
Basically Cocoapods is a Ruby Gem, so you will need Ruby installed. Right now Ruby is installed in macOS by default, but to be sure you use the latest version it’s best to install also RVM (Ruby Version Manager).
When you have the latest Ruby version, just run in console:
$ gem install cocoapods
Successfully installed cocoapods-1.1.1 Parsing documentation for cocoapods-1.1.1 Done installing documentation for cocoapods after 2 seconds 1 gem installed
When you have
RVM installed, gems will be stored in your home directory. So you don’t have to use
sudo to install
The latest version of
CocoaPods will be installed, which is generally good, but sometimes you need to install a specific version (because of a bug) or you have an old project which hasn’t been updated for a long time. To install a different version of
CocoaPods, install Bundler first:
$ gem install bundler
Fetching: bundler-1.13.7.gem (100%) Successfully installed bundler-1.13.7 Parsing documentation for bundler-1.13.7 Installing ri documentation for bundler-1.13.7 Done installing documentation for bundler after 5 seconds 1 gem installed
Bundler will help us to install the exact gem version that we need. Create file
Gemfile and put this inside:
source 'https://rubygems.org' gem 'cocoapods', '1.1.1'
Then just run:
$ bundle install
Now we can execute gems using
bundle exec command. To start with
Cocoapods run this command:
bundle exec pod init
It will create a default
Podfile with targets that are included inside the project:
# Uncomment the next line to define a global platform for your project # platform :ios, '9.0' target 'iOSProjectStarter' do # Comment the next line if you're not using Swift and don't want to use dynamic frameworks use_frameworks! # Pods for iOSProjectStarter target 'iOSProjectStarterSpec' do inherit! :search_paths # Pods for testing end end
Libraries that we want to install right now are just:
Podfile a little bit and put this:
platform :ios, '10.0' def testing_pods pod 'Quick' pod 'Nimble' end target 'iOSProjectStarter' do use_frameworks! # Pods for iOSProjectStarter pod 'Reveal-iOS-SDK', :configurations => ['Debug'] end target 'iOSProjectStarterSpec' do use_frameworks! testing_pods end
platform - specifies a platform for which a static library should be built. Here we use only
iOS, but you can also choose:
def testing_pods - you can define sets of pods to reuse in different places (remember it’s basically a ruby file here).
target - it simply specifies target of the project.
use_frameworks! - it tells to integrate pods to your project via frameworks instead of static libraries.
iOSProjectStarter we will use only one pod for now:
Reveal-iOS-SDK. Don’t forget to set its build configurations to
Debug, otherwise in future when you push your app to AppStore, it will immediately throw an error about using private api.
testing_pods which includes
That’s all about configuration at this point, let’s install it. When you install CocoaPods for the first time just run:
pod install, you should also use it when you add new pods to
Podfile. To update pods that are already there just run:
$ bundle exec pod install
Analyzing dependencies Downloading dependencies Installing Nimble (5.0.0) Installing Quick (0.10.0) Installing Reveal-iOS-SDK (1.6.2) Generating Pods project Integrating client project Sending stats Pod installation complete! There are 3 dependencies from the Podfile and 3 total pods installed. [!] The `iOSProjectStarterSpec [Debug]` target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target Support Files/Pods-iOSProjectStarterSpec/Pods-iOSProjectStarterSpec.debug.xcconfig'. This can lead to problems with the CocoaPods installation - Use the `$(inherited)` flag, or - Remove the build settings from the target. [!] The `iOSProjectStarterSpec [Release]` target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target Support Files/Pods-iOSProjectStarterSpec/Pods-iOSProjectStarterSpec.release.xcconfig'. This can lead to problems with the CocoaPods installation - Use the `$(inherited)` flag, or - Remove the build settings from the target.
Cocoapods version sometimes you may encounter some warnings or even an error. You should have noticed above that I’ve got two warnings. You can read about those here.
To fix this, click
iOSProjectStarter project inside Project navigator, then inside target
Build Settings and search for key
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES. It will be set to
Yes, just click on it and change to
Other... then replace text with
$(inherited) and install pods once again.
As you can see installing
CocoaPods is a very simple task.
In the next step we will change and reorganize directory structure.