Code Samples Overview

The SDK demo apps supplied with the Places SDK for iOS demonstrate a number of features including the place picker, place autocomplete, and place photos. In addition, you can find code snippets on each page of this developer's guide.

Try the SDK demos using CocoaPods

The Places SDK for iOS is available as a CocoaPods pod. CocoaPods is an open source dependency manager for Swift and Objective-C Cocoa projects.

The Places SDK for iOS supplies a set of SDK demo apps that you can install and run using the pod try command:

Follow these steps to install CocoaPods and try the Places SDK for iOS:

  1. 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
  2. Open a terminal window and run one of the following commands:
    • pod try GooglePlaces
    • pod try GooglePlacePicker

    CocoaPods updates your spec repositories, then opens the SDK demos in a temporary Xcode project, either GooglePlacesDemos.xcworkspace or GooglePlacePickerDemos.xcworkspace, depending on which demo you have chosen.

  3. Enable both the Places SDK for iOS and Maps SDK for iOS for your Google Cloud Platform Console project.
  4. If you don't yet have an API key, follow the instructions to set up a project on the Google Cloud Platform Console and get an API key. When configuring the key on the Google Cloud Platform Console, you can specify your app's bundle identifier to ensure that only your app can use the key. The default bundle identifier of the SDK samples app is either com.example.GooglePlacesDemos or com.example.GooglePlacePickerDemos, depending on which demo you have chosen.
  5. Edit the SDKDemoAPIKey file and paste your API key into the appropriate constant. In the case of the place picker demo this is the kPlacesAPIKey and kMapsAPIKey constants. For example:
    internal let kPlacesAPIKey = "YOUR_API_KEY"
    internal let kMapsAPIKey = "YOUR_API_KEY"
  6. If Xcode prompts you to unlock the SDKDemoAPIKey file for editing, choose Unlock.
  7. If present, remove the following line, because it's used to register the issue:
    #error Register your API key and insert here, then delete this line.
  8. Build and run the project again.
  9. If the build fails or the app crashes with an error about your API key when you first run it, double check to make sure that you have provided the keys required in the SDKDemoAPIKey file.
  10. If you are running the GooglePlaces demos, the iOS simulator window will show a list of Places Demos.
  11. If prompted to allow GooglePlacesDemos or GooglePlacePickerDemos to access your location, choose Allow.
  12. The sample you chose is now ready to run.

Use code snippets from the developer's guide

Each page in the developer's guide includes code snippets illustrating a particular feature of the API. For example, see the guides for place picker, autocomplete, get current place, photos, and other pages in this guide.

Envoyer des commentaires concernant…