Using CocoaPods in iOS/MacOS Development: A Step-by-Step Guide

Ihor Malovanyi
4 min readAug 8, 2023

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. With over 100,000 libraries and counting, it’s become an essential tool for many iOS developers. This guide will walk you through using CocoaPods in your iOS/MacOS projects.

Before starting reading, make sure you are not russian. By continuing reading, you confirm that you condemn the war in Ukraine unleashed by russia.

Glory to Ukraine! 🇺🇦

Prerequisites

To use CocoaPods, make sure you have the following:

  • A Mac running macOS.
  • Xcode installed.
  • Terminal access (you’ll be running several commands).

Installing CocoaPods

If you haven’t installed CocoaPods yet, you can do so by running the following command in your terminal:

sudo gem install cocoapods

This installs CocoaPods using RubyGems, which comes pre-installed with macOS.

Setting Up CocoaPods for Your Project

Navigate to your project’s directory using the terminal. If you haven’t already created an Xcode project, do so.

Inside the project directory, initialize CocoaPods by running:

pod init

This will create a Podfile in your project directory.

Adding Dependencies

Open the Podfile using a text editor or type open -a Xcode Podfile to open it with Xcode. Or you can use a text editor to open and modify the file.

To add a pod, you list it in the Podfile. For example, to add Alamofire:

target 'YourProjectName' do
use_frameworks!

# Pods for YourProjectName
pod 'Alamofire', '~> 5.7'
end

Replace ‘YourProjectName’ with the actual name of your project.

Setup Podfile is another big topic, and now I wouldn’t stop at that point. But you can read an official page if you want to dive deep inside the Podfile structure.

Installing Pods

After you’ve added all desired pods, go back to your terminal and run:

pod install

This will create a .xcworkspace file in your directory. From now on, open the .xcworkspace file (not .xcodeproj) to work on your project in Xcode.

Behind the scenes

After you run the pod install command, your project transforms with the following operations:

  • Creates or updates workspace.
  • Adds your project to the workspace.
  • Adds CocoaPods static library to the workspace.
  • Adds libPods.a to: targets => build phases => link with libraries.
  • Adds the CocoaPods Xcode configuration file to your app’s project.
  • Changes your app’s target configurations to be based on CocoaPods.
  • Adds a build phase to copy resources from any pods you installed to your app bundle. i.e., a ‘Script build phase’ after all other build phases with the following:
  • - Shell: /bin/sh
  • - Script: ${SRCROOT}/Pods/PodsResources.sh

As a result, you achieve the next project structure:

  • The Pods project includes libraries described in Podfile.
  • Podfile.lock file is a snapshot of exact pod versions used in a CocoaPods project, ensuring consistency across installations.
  • .xworkspace file is a workspace file that contains your project and CocoaPods project and the necessary additional settings to make them work together.

Updating Pods

Over time, the libraries you use might get updated. To update a specific pod:

pod update PODNAME

To update all pods:

pod update

Removing Pods

To remove a pod, simply delete (or comment out) the line from the Podfile and then run:

pod install

If you want to clear your project and remove all pods and depending on settings, run the following command:

pod deintegrate

Best Practices

  • Committing Pods to Version Control: Some developers prefer committing installed pods to version control (e.g., Git) to ensure everyone on the team has the same version. Others prefer to exclude the Pods directory and only commit the Podfile and Podfile.lock.
  • Private Pods: You can create private podspecs and repos for proprietary libraries.
  • Always Use the Workspace: Once you’ve initiated CocoaPods, use the .xcworkspace file.

Conclusion

CocoaPods is a powerful tool for managing third-party libraries in iOS/MacOS development. It simplifies the process of integrating, updating, and managing dependencies. With the basic knowledge from this guide, you’re now ready to streamline your iOS/MacOS development process using CocoaPods.

One more thing…

If you like my content and want to support me, please buy me a coffee and subscribe!

Also, I have the Telegram channel Swift in UA, where you can find more exciting things.

Thank you for reading. See you soon!

--

--