What is CocoaPods for Swift development?

Share

CocoaPods

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.

Installation

  • You need to have Homebrew pre-installed on your Mac.

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.

Adding CocoaPods to a Swift project

  • 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.

  • Your 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
  • Now, to add your desired pod, simply visit the website, and then search for the needed package.

Example

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
  • Now, open the project root within your Terminal again, and run the following:
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.

Troubleshooting for Apple Silicon / M1 Mac users

  • 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:

  1. Simply clone your Terminal and rename it as “Rosetta-Terminal”.
  2. Right-click on your Rosetta-Terminal and click on “Get Info”.
  3. Check the option “Open with Rosetta”.
  • Now, you can use this Rosetta Terminal to create and run all the needed CocoaPods operations on the project. Additionally, you can view if any package or toolkit is supported on your M1 Mac, or not, on this website.