In this post I will show how to Install Cocoa Pods for Xcode projects. Cocoa Pods is a dependency manager for Swift and Objective-C Cocoa projects.
Installation
When you install XCode, you also get the gem
(RubyGems, the Ruby package manager). This command allows you to install Cocoa Pods for Xcode, without any extra configuration. Execute the following command
sudo gem install cocoapods
After fetching a list of dependency packages, you can use cocoa pods calling the command pod
. However, each command must be executed in the root folder of your Xcode project:
pod init
: To create an empty initialPodFile
in your Xcode project folder.pod install
: To install packages defined in thePodFile
Using Cocoa Pods command line
Create your Xcode application as usual. Then, after you install Cocoa Pods, you can create the initial PodFile
executing pod init
inside the root folder of your application. After that, you will get aPodFile
like this:
# Uncomment the next line to define a global platform for your project # platform :ios, '9.0' target 'my-app-name' do # Comment the next line if you're not using Swift and don't want to use dynamic frameworks use_frameworks! # Pods for my-app-name end
If you want to add some pod, like the Google Maps iOS SDK components, you can add it to the section target
:
target 'my-app-name' do pod 'GoogleMaps' pod 'GooglePlaces' end
Then, you can install the pods defined in the PodFile using the command pod install
.
The first time you run pod install
, the system will retrieve the master repository of Cocoa Pods. This will retrieve a local reference of all Cocoa pods available. It could take a couple of minutes to complete.
Then, you will receive a message of a successful installation of pods:
Analyzing dependencies
Downloading dependencies
Using GoogleMaps (2.7.0)
Using GooglePlaces (2.7.0)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.
[!] Automatically assigning platform `ios` with version `12.0` on target `ios-googlemaps-app` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
To avoid the warning message about the platform version, you may want to edit the Podfile
. Uncomment the platform
definition or creating a new one. Generally, use the last available platform, unless the project or the component has some platform version restrictions:
platform :ios, '12.0'
Your Podfile
contents will lok like this one
platform :ios, '12.0' target 'ios-googlemaps-view' do use_frameworks! pod 'GoogleMaps' pod 'GooglePlaces' end
Using Cocoa Pods Components in Xcode
Open the Xcode project workspace
Running the command pod install will create a new Xcode workspace to manage your project. From now, you need to open your project from the .xcworkspace (Example: ios-googlemaps-view.xcworkspace
).
Open the .xcworkspace
file from Finder or call open your-project-name.xcworkspace
from command line. If you open the .xcodeproj
file, you will not be able to compile your project with cocoa pods references.
Xcode Project structure
Now your workspace contains 2 projects, your Application itself, and the Pods
project. The second one is to manage your pods, to compile and link with your project.
Add a Custom Cocoa component
Adding a custom component is simple.
- Start adding an
UIView
to your application view. - Configure the contraints and position
- Configure a custom Class name (Example:
GMSMapView)
Attach your component to the code
Follow this steps to attach your component to the code as an @IBOutlet
and complete the imports:
- Open an assistant editor (double circle button) along with the storyboard side-by-side
- Right click over the component and drag to the ViewController code window
- Assign a variable name (example:
mapView
) - Add import for
GoogleMaps
library - Additionally, modify
AppDelegate
to configure API keys for Google Maps and Google Places API if needed.
From this point, you will be able to work in your application as usual.
Originally Posted in https://developerhowto.com/2018/11/06/cocoa-pods-for-xcode/