Get an API Key

To use the Maps SDK for iOS you must have an API key. The API key is a unique identifier that is used to authenticate requests associated with your project for usage and billing purposes.

Get the API key

You must have at least one API key associated with your project.

To get an API key:

  1. Visit the Google Cloud Platform Console.
  2. Click the project drop-down and select or create the project for which you want to add an API key.
  3. Click the menu button and select APIs & Services > Credentials.
  4. On the Credentials page, click Create credentials > API key.
    The API key created dialog displays your newly created API key.
  5. Click Close.
    The new API key is listed on the Credentials page under API keys.
    (Remember to restrict the API key before using it in production.)

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:

  1. Add the following import statement:
    import GoogleMaps
  2. Add the following to your application(_:didFinishLaunchingWithOptions:) method, replacing YOUR_API_KEY with your API key:
    GMSServices.provideAPIKey("YOUR_API_KEY")
  3. If you are also using the Places API, add your key again as shown here:
    GMSPlacesClient.provideAPIKey("YOUR_API_KEY")

Objective-C

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

  1. Add the following import statement:
    @import GoogleMaps;
  2. Add the following to your application:didFinishLaunchingWithOptions: method, replacing YOUR_API_KEY with your API key:
    [GMSServices provideAPIKey:@"YOUR_API_KEY"];
  3. If you are also using the Places API, add your key again as shown here:
    [GMSPlacesClient provideAPIKey:@"YOUR_API_KEY"];

Restrict the API key

We strongly recommend that you restrict your API key. Restrictions provide added security and help ensure only authorized requests are made with your API key. There are two restrictions. You should set both:

  • Application restriction: Limits usage of the API key to a specific web site (HTTP referrers), web server (IP addresses), or mobile app (Android apps or iOS apps). You can select only one restriction from this category (see GMP APIs by Platform).
  • API restriction: Limits usage of the API key to one or more APIs or SDKs. Requests to an API or SDK associated with the API key will be processed. Requests to an API or SDK not associated with the API key will fail. (The API or SDK must be enabled and must support the application restriction.)

To restrict an API key:

  1. Go to the Google Cloud Platform Console.
  2. Click the project drop-down and select the project that contains the API key you want to secure.
  3. Click the menu button and select APIs & Services > Credentials.
  4. On the Credentials page, click the name of the API key that you want to secure.
  5. On the Restrict and rename API key page, set the restrictions:
    • Application restrictions
      • Select iOS apps.
      • Add your app's bundle identifier.
    • API restrictions
      • Select Restrict key.
      • Click Select APIs and select Maps SDK for iOS.
        (If the Maps SDK for iOS is not listed, you need to enable it.)
  6. Click SAVE.

Premium Plan customers

Follow these steps to get an API key and add restrictions:

  1. Go to the Google Cloud Platform Console.
  2. Click the project drop-down and select the project created for you when you purchased theGoogle Maps APIs Premium Plan. The project name starts with Google Maps APIs for Business or Google Maps for Work or Google Maps.
  3. Click the menu button and select APIs & Services > Credentials.
  4. On the Credentials page, click Create credentials > API key.
    The API key created dialog displays your newly created API key.
  5. Click Close.
    The new API key is listed on the Credentials page under API keys.
    (To continue, see Add the API key to your app and Restrict the API key.)

Enviar comentarios sobre...

Si necesitas ayuda, visita nuestra página de asistencia.