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:
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
Save & on the terminal type
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.
Remove pods from project:
- sudo gem install cocoapods-deintegrate cocoapods-clean
- pod deintegrate
- pod cache clean --all
- rm Podfile
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