Set up an Xcode project

After you enable billing and create an API key, you’re ready to set up the Xcode project that you use to develop your app.

Release notes are available for each release.

Step 1: Install the required software

To build a project using the Places SDK for iOS, you need:

  • Xcode version 14.0 or later

Step 2: Create the Xcode project and install the Places SDK for iOS

Swift Package Manager

The Places SDK for iOS can be installed via Swift Package Manager. To add the SDK, ensure you have removed any existing Places SDK for iOS dependencies.

To add the SDK to a new or existing project, follow these steps:

  1. Open your Xcode project or workspace, then go to File > Add Package Dependencies.
  2. Enter https://github.com/googlemaps/ios-places-sdk as the URL, press Enter to pull in the package, and click "Add Package".
  3. To install a specific version, set the Dependency Rule field to one of the version-based options. For new projects, we recommend specifying the latest version and using the "Exact Version" option. Once complete, click "Add Package".
  4. From the Choose Package Products window, verify GooglePlaces will be added to your designated main target. Once complete, click "Add Package".
  5. To verify your installation, navigate to your target's General pane. In the Frameworks, Libraries, and Embedded Content you should see the installed packages. You can also view the "Package Dependencies" section of "Project Navigator" to verify the package and its version.

To update the package for an existing project, follow these steps:

  1. From Xcode, go to "File > Packages > Update To Latest Package Versions".
  2. To verify your installation, go to the Package Dependencies section of Project Navigator to verify the package and its version.

To remove existing Places SDK for iOS dependencies added using CocoaPods, follow these steps:

  1. Close your Xcode workspace. Open terminal and execute the following command:
    sudo gem install cocoapods-deintegrate cocoapods-clean 
    pod deintegrate 
    pod cache clean --all
  2. Remove the Podfile, Podfile.resolved and the Xcode workspace if you are not using them for anything other than CocoaPods.

To remove existing Places SDK for iOS installed manually, follow these steps:

  1. From your Xcode project configuration settings, find Frameworks, Libraries, and Embedded Content. Use the minus sign(-) to remove the following framework:
    • GooglePlaces.xcframework
  2. From the top level directory of your Xcode project, remove the GooglePlaces bundle.

Cocoapods

The Places SDK for iOS is available as a CocoaPod pod, GooglePlaces, which contains all places functionality.

CocoaPods is an open source dependency manager for Swift and Objective-C Cocoa projects. If you don't already have the CocoaPods tool, install it on macOS by running the following command from the terminal. For details, see the CocoaPods Getting Started guide.

sudo gem install cocoapods

Create a Podfile for the Places SDK for iOS and use it to install the SDK and its dependencies:

  1. If you don't have an Xcode project yet, create one now and save it to your local machine. If you're newer to iOS development, create a new project and select the iOS App template.
  2. Create a file named Podfile in your project directory. This file defines your project's dependencies.
  3. Edit the Podfile and add your dependencies along with their versions. Here is an example which specifies your application target name, and the name of the GooglePlaces pod:
    source 'https://github.com/CocoaPods/Specs.git'
    
    platform :ios, '14.0'
    
    target 'YOUR_APPLICATION_TARGET_NAME_HERE' do
      pod 'GooglePlaces', '8.3.0'
    end
    
    Make sure to regularly run pod outdated to detect when there is a newer version to ensure you're always on the latest.
  4. Save the Podfile.
  5. Open a terminal and go to the directory containing the Podfile:

    cd <path-to-project>
  6. Run the pod install command. This will install the APIs specified in the Podfile, along with any dependencies they may have.

    pod install
  7. Close Xcode, and then open (double-click) your project's .xcworkspace file to launch Xcode. From this time onwards, you must use the .xcworkspace file to open the project.

To update the API for an existing project, follow these steps:

  1. Open a terminal and go to the project directory containing the Podfile.
  2. Run the pod update command. This will update all of the APIs specified in the Podfile to the latest version.

Manual installation

This guide shows how to manually add the XCFramework containing the Places SDK for iOS to your project and configure your build settings in Xcode. An XCFramework is a binary package that you can use on multiple platforms, including machines using Apple silicon.

  1. Download the following SDK binary and resource files:
  2. Unpack the zipped files to access the XCFramework and resources.
  3. Launch Xcode and either open an existing project, or create a new project. If you're newer to iOS development, create a new project and select the iOS App template.
  4. Remove any Maps bundles from previous releases from your project.
  5. Open the General tab. Drag the following XCFramework into your project under Frameworks, Libraries, and Embedded Content. Make sure to select Do Not Embed:
    • GooglePlaces.xcframework
  6. Drag GooglePlaces.bundle from GooglePlacesResources you downloaded
  7. Copy the GooglePlaces.bundle from the GooglePlacesResources you downloaded into your Xcode project's top level directory. Make sure to select Copy items into destination group's folder when prompted.
  8. Select your project from the Project Navigator, and choose your application's target.
  9. Open the Build Phases tab. Within Link Binary with Libraries, add the following frameworks and libraries:
    • CoreGraphics.framework
    • CoreLocation.framework
    • libc++.tbd
    • libz.tbd
    • QuartzCore.framework
    • UIKit.framework
  10. Choose your project, rather than a specific target, and open the Build Settings tab. In the Linking - General -> Other Linker Flags section, add -ObjC to "Debug" and "Release". If these settings aren't visible, change the filter in the Build Settings bar from Basic to All.

Step 3: Add the API key to your app

In the following examples, replace YOUR_API_KEY with your API key.

Swift

Add your API key to your AppDelegate.swift as follows:

  • Add the following import statement:
    import GooglePlaces
  • Add the following to your application(_:didFinishLaunchingWithOptions:) method, replacing YOUR_API_KEY with your API key:
    GMSPlacesClient.provideAPIKey("YOUR_API_KEY")

Objective-C

Add your API key to your AppDelegate.m as follows:

  • Add the following import statement:
    @import GooglePlaces;
  • Add the following to your application:didFinishLaunchingWithOptions: method, replacing YOUR_API_KEY with your API key:
    [GMSPlacesClient provideAPIKey:@"YOUR_API_KEY"];

Step 4: Start writing code

The following code samples demonstrate how to get the current place.

Swift

import GooglePlaces
import UIKit

class GetStartedViewController : UIViewController {

  // Add a pair of UILabels in Interface Builder, and connect the outlets to these variables.
  @IBOutlet private var nameLabel: UILabel!
  @IBOutlet private var addressLabel: UILabel!

  private var placesClient: GMSPlacesClient!

  override func viewDidLoad() {
    super.viewDidLoad()
    placesClient = GMSPlacesClient.shared()
  }

  // Add a UIButton in Interface Builder, and connect the action to this function.
  @IBAction func getCurrentPlace(_ sender: UIButton) {
    let placeFields: GMSPlaceField = [.name, .formattedAddress]
    placesClient.findPlaceLikelihoodsFromCurrentLocation(withPlaceFields: placeFields) { [weak self] (placeLikelihoods, error) in
      guard let strongSelf = self else {
        return
      }

      guard error == nil else {
        print("Current place error: \(error?.localizedDescription ?? "")")
        return
      }

      guard let place = placeLikelihoods?.first?.place else {
        strongSelf.nameLabel.text = "No current place"
        strongSelf.addressLabel.text = ""
        return
      }

      strongSelf.nameLabel.text = place.name
      strongSelf.addressLabel.text = place.formattedAddress
    }
  }
}
      

Objective-C

#import "GetStartedViewController.h"
@import GooglePlaces;

@interface GetStartedViewController ()
// Add a pair of UILabels in Interface Builder and connect the outlets to these variables
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@end

@implementation GetStartedViewController {
  GMSPlacesClient *_placesClient;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  _placesClient = [GMSPlacesClient sharedClient];
}

// Add a pair of UILabels in Interface Builder and connect the outlets to these variables.
- (IBAction)getCurrentPlace:(UIButton *)sender {
  GMSPlaceField placeFields = (GMSPlaceFieldName | GMSPlaceFieldFormattedAddress);

  __weak typeof(self) weakSelf = self;
  [_placesClient findPlaceLikelihoodsFromCurrentLocationWithPlaceFields:placeFields callback:^(NSArray<GMSPlaceLikelihood *> * _Nullable likelihoods, NSError * _Nullable error) {
    __typeof__(self) strongSelf = weakSelf;
    if (strongSelf == nil) {
      return;
    }

    if (error != nil) {
      NSLog(@"An error occurred %@", [error localizedDescription]);
      return;
    }

    GMSPlace *place = likelihoods.firstObject.place;
    if (place == nil) {
      strongSelf.nameLabel.text = @"No current place";
      strongSelf.addressLabel.text = @"";
      return;
    }

    strongSelf.nameLabel.text = place.name;
    strongSelf.addressLabel.text = place.formattedAddress;
  }];
}

@end
      

Next steps

After your project is configured, you can explore the sample apps. You'll need Cocoapods v1.6.1 installed.