Cocoapods is a Swift and Objective-C dependency management tool like NPM is for Node and Gem is for Ruby. Swift now has its own official package manager. But it’s still very new. Until it matures fully you can keep using Cocoapods.
Use Gem to install Cocoapods
sudo gem install cocoapods
A few things may go wrong.
You may see this error:
ERROR: While executing gem ... (Errno::EPERM) Operation not permitted
If you see that try installing Cocoapod like this:
sudo gem install -n /usr/local/bin cocoapods
You might see a Insecure world writable dir warning. This can be safely ignored.
Create an Xcode project if you don’t already have one. Here, we will assume that the name of your project is
MyApp. In the root folder of the project create a file called Podfile.
platform :ios, '9.0' use_frameworks! target 'MyApp' do pod 'AFNetworking', '~> 2.0' pod 'FLKAutoLayout' end
This states that our project requires
2.0 or higher. It also depends on the latest version of
When you install a package Cocoapod downloads the source files of the package and any other packages that it depends on. Cocoapod creates a new project called
Pods and adds these source files there. It then creates a new workspace and adds your project and the
Pods project to the workspace.
Since Cocoapod modifies your Xcode workspace, it is better that you shutdown Xcode while you are installing a package.
Make sure that your Podfile is properly defined. From the root folder of your project run:
There are a few things you have to do for your project to compile correctly:
- Shutdown Xcode if it is running.
- Double click the workspace file to open it and not the project file. In our case that will be the
Now you will see the
Pods project added to the workspace. Source code for the downloaded packages will be there.
Cocoapod will also modify your project to link in the code from all packages in the
You are now ready to use classes from these packages.
To remove a package simply delete it from Podfile. Then from the root of the project run:
Using Pod Packages in Unit Tests
So far in this article we have added packages only to the application’s main target. If you try to use these packages from unit tests you will get a compilation error. The solution is to also add the packages to the unit test targets. Find out the name of the unit test target and specify the packages used from there.
platform :ios, '9.0' use_frameworks! target 'MyApp' do pod 'AFNetworking', '~> 2.0' pod 'FLKAutoLayout' end #Add any package you need to the unit test target target 'MyAppTests' do pod 'AFNetworking', '~> 2.0' pod 'FLKAutoLayout' end
pod update to set things up.