CocoaPods is a third-party dependency manager for Swift projects. With the help of CocoaPods, developers can use code that was already created and prepped by other developers to create simple and easy code, instead of having to code everything from scratch.
Homebrew is a missing package manager for macOS. You can read about installing Homebrew here.
Homebrew will also download a package named gem
; a Ruby-based package manager and is a pre-requisite for CocoaPods installation.
Now, you can simply install CocoaPods using:
sudo gem install cocoapods
You can visit https://cocoapods.org for any troubleshooting during installation.
First, close your Xcode project and navigate to the project root using Terminal.
Run pod init
to initiate CocoaPods within the project root. This will also create a Podfile
within the root, which is the CocoaPods configuration for the project.
For users with M1/Apple Silicon Macs, read the troubleshooting steps below.
Podfile
should look somewhat like this:# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'testproject' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for testproject
end
We will be using Alamofire for this example, which is an HTTP-based package for Elegant Networking in Swift.
You can find the installation for Alamofire using CocoaPods, which is done by adding the following line:
pod 'Alamofire', '~> 5.2'
Note: This installation line differs from package to package and can be viewed (for each package) within the website.
This line is to be added exactly under the Pods
section of the Podfile
.
Note:
Podfile
is highly indentation-sensitive.
Your Podfile
will now look like this:
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'testproject' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for testproject
pod 'Alamofire', '~> 5.2'
end
pod install
This will install the needed package inside your Xcode project, and will also create a .xcworkspace
file within the root. You can use the .xcworkspace
file from now on instead of your .xcodeproj
.
For installing other pods, add other pods in exactly the next line with the same indentation.
CocoaPods is not yet available for native M1 users as of 11th July 2021; however, it is supported through Rosetta 2. Rosetta 2 is a virtualization-based CLI software by Apple Inc. that allows users to run Intel architecture apps on ARM architecture, i.e., Apple Silicon/M1.
The installation steps will all remain the same, but with a little pre-requisite. You’ll need to run your terminal with Rosetta 2.
To do this: