How to use CocoaPods

From time to time I forget how to install or use CocoaPods, so in this guide I'll cover how to install it and how to use it.

What is CocoaPods

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over 79 thousand libraries and is used in over 3 million apps. CocoaPods can help you scale your projects elegantly.

How to install CocoaPods

CocoaPods is built with Ruby and is installable with the default Ruby available on macOS, you can installing it by typing on the terminal:

sudo gem install cocoapods

How to use CocoaPods

In your terminal, navigate to the root directory of your project, for example: a project called Planner, you'll see 2 files:

In that directory, type:

pod init

if you look again, you'll see a new file called Podfile

Open that Podfile and add your pod dependencies, example of adding Google Analytics:

platform :ios, '14.0'

target 'Planner' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for Planner

  # add the Firebase pod for Google Analytics
  pod 'Firebase/Analytics'
  # add pods for any other desired Firebase products
  # https://firebase.google.com/docs/ios/setup#available-pods

end

Install Pods

For non-M1 mac users you can simply do:

pod install

For M1 mac users first make sure you are project is clean out of pods installs. Then do:

sudo arch -x86_64 gem install ffi
arch -x86_64 pod install

# From this point it's all Optional - Creating an alias for arch -x86_64
# Add it in your ~/.bash_profile, ~/.bashrc or ~/.zshrc:

# Intel Platform (M1)
alias intel="arch -x86_64"

# How to use the alias:
intel pod install
This creates an .xcworkspace file for your app. Use this file for all future development on your application.

CocoaPod dependencies Maintenance

List of commands that may come handy at some point.

Update dependencies: pod update

Remove pods from project:

  1. sudo gem install cocoapods-deintegrate cocoapods-clean
  2. pod deintegrate
  3. pod cache clean --all
  4. rm Podfile

Troubleshooting

If you see an error like this: The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported deployment target versions is 9.0 to 14.3.99. that's because the pods is trying to use a different simulator version than what your project is using. Fix it adding this to your Podfile:

post_install do |installer|
 installer.pods_project.targets.each do |target|
  target.build_configurations.each do |config|
   config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '9.0'
  end
 end
end