16

I am trying to implement Google Maps SDK into my project using Swift 2.0. I follow this but when running this, my app is getting the following error:

2015-08-25 19:05:17.337 googleMap[1919:54102] *** Terminating app due to uncaught exception 'GMSServicesException', reason: 'Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use
*** First throw call stack:
(
    0   CoreFoundation    0x00000001058499b5 __exceptionPreprocess + 165
...
...
...
    31  UIKit             0x000000010606699e UIApplicationMain + 171
    32  googleMap         0x00000001034b720d main + 109
    33  libdyld.dylib     0x0000000107fba92d start + 1
    34  ???               0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I have tried all possible solutions from StackOverflow.

Naresh
  • 16,698
  • 6
  • 112
  • 113
iBeginner
  • 241
  • 1
  • 2
  • 8
  • You see my answer, you need to add GMSPlacesClient.provideAPIKey("Your key") in appdelegate – Naresh Feb 23 '18 at 10:41

12 Answers12

34

You need to set this both in didFinishLaunchingWithOptions

GMSPlacesClient.provideAPIKey("Your key")
GMSServices.provideAPIKey("Your key")
Jed Fox
  • 2,979
  • 5
  • 28
  • 38
  • Also there is [migration note](https://developers.google.com/places/migrate-to-v2) from Google. – mixel Mar 09 '17 at 15:54
22

Just adding to Rajasekaran Gopal

Remember to add

import GoogleMaps
import GooglePlaces

then

GMSPlacesClient.provideAPIKey("Your key")
GMSServices.provideAPIKey("Your key")

in didFinishLaunchingWithOptions

Serjik
  • 10,543
  • 8
  • 61
  • 70
Vu Tran
  • 221
  • 2
  • 3
17

I just had the same problem. I created a GMSMapView object and it was initialized before maybe the api key could be read. So I moved it inside the viewDidLoad method and problem solved.

Before :

class ViewController: ..... {

let mapView = GMSMapView()

After :

class ViewController: ..... {

var mapView : GMSMapView?

override viewDidLoad(){
    mapView = GMSMapView()
Jasveer
  • 209
  • 2
  • 8
5

Here is a Google Maps tutorial for Swift:

http://www.appcoda.com/google-maps-api-tutorial/

Below is quoted from Google Maps Documentation:

Step 5: Get an iOS API key

Using an API key enables you to monitor your application's API usage, and ensures that Google can contact you about your application if necessary. The key is free, you can use it with any of your applications that call the Google Maps SDK for iOS, and it supports an unlimited number of users. You obtain an API key from the Google Developers Console by providing your application's bundle identifier.

If your project doesn't already have a key for iOS applications, follow these steps to create an API key from the Google Developers Console:

  1. In the sidebar on the left, select Credentials.
  2. If your project doesn't already have an iOS API key, create one now by selecting Add credentials > API key > iOS key.
  3. In the resulting dialog, enter your app's bundle identifier. For example: com.example.hellomap.
  4. Click Create.

    Your new iOS API key appears in the list of API keys for your project. An API key is a string of characters, something like this:

    AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0

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

    • Add the following import statement:

    @import GoogleMaps;

    • Add the following to your application:didFinishLaunchingWithOptions: method, replacing API_KEY with your API key:

    [GMSServices provideAPIKey:@"API_KEY"];

Source

emotality
  • 12,795
  • 4
  • 39
  • 60
  • 9
    Yes I did all this..but still getting that exception – iBeginner Aug 26 '15 at 08:25
  • 1
    Yes I have linked all those which are mentioned in above link. – iBeginner Aug 26 '15 at 08:30
  • @iBeginner I am facing the same issue. Can you elaborate your solution in detail. – Arasuvel Jan 11 '16 at 06:20
  • 3
    I'm actually seeing the same thing. I even get a `true` response from `provideAPIKey`, well beyond trying to _use_ the API. This looks like a bug in the framework, currently using version `1.11.1` – Craig Otis Mar 15 '16 at 04:07
  • @CraigOtis Any solution to this? I've enabled the API, I've generated the key, I get `true` when I use `provideAPIKey` as well. I'm out of ideas. – JCutting8 Aug 21 '22 at 13:17
4

If you already set GMSMapView class to your view in interface builder, make GMSServices.provideAPIKey("API key") in initialization methods of viewController which contains GMSMapView.

MinnuKaAnae
  • 1,646
  • 3
  • 23
  • 35
Pavel Bondar
  • 41
  • 1
  • 4
4

You should set up API keys before you set up the UIWindow in AppDelgate, if your initial View Controller uses GoogleMaps.

For example:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        //MARK: Google Maps setup
        GMSServices.provideAPIKey("YOUR API KEY")
        GMSPlacesClient.provideAPIKey("YOUR API KEY")

        let window = UIWindow(frame: UIScreen.main.bounds)
        window.backgroundColor = UIColor.white
        window.makeKeyAndVisible()
        window.rootViewController = ViewController()
        self.window = window
        return true
    }
popic.m
  • 41
  • 1
2

In my app i provided key to GMSPlacesClient in AppDelegate -didFinishLaunchingWithOptions

Ex:
@import GooglePlaces;
@import GoogleMaps; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [GMSServices provideAPIKey:@"You key"];
    [GMSPlacesClient provideAPIKey:@"Your key"];
}

For Swift:

import GoogleMaps
import GooglePlaces

//in application(_:didFinishLaunchingWithOptions:)

GMSServices.provideAPIKey("YOUR_API_KEY")
GMSPlacesClient.provideAPIKey("YOUR_API_KEY")
Naresh
  • 16,698
  • 6
  • 112
  • 113
1

in my case --didFinishLaunchingWithOptions-- method didn't call because i updated swift version. first make sure this method called normally. and in my case i didn't need to GMSPlacesClient.provideAPIKey("Your key"). however you can initial GMSServices in your initial viewcontroller (if its not contain map or related objet to it)

iman kazemayni
  • 1,255
  • 1
  • 19
  • 20
1

You are skipping provideAPIKey in your AppDelegate check project

Your_PROJECT -> ios -> Runner -> AppDelegate.m

Replace your file code with below snippet add your APIKEY

#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
#import "GoogleMaps/GoogleMaps.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GMSServices provideAPIKey:@"GOOGLE_API_KEY"];
  [GeneratedPluginRegistrant registerWithRegistry:self];
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end

Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...]

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Hemant Sharma
  • 259
  • 5
  • 14
  • 1
    As question was tagged as "swift", so the objc code snippet is not an appropriate answer. – pomo_mondreganto Jan 15 '19 at 10:54
  • I have already work on this topic when i was stuck in this issue i have seen this post also, i have found solution on other else website where i have post my question related to this and share here what i have found – Hemant Sharma Mar 05 '19 at 07:51
1

XCODE 12.0.1, Swift 5

I was applying constraints to Google Map View by initialising GMSMapView like this:-

private var googleMapsView : GMSMapView = {
    let mainView = GMSMapView()
    mainView.translatesAutoresizingMaskIntoConstraints = false
    mainView.isMyLocationEnabled = true
    return mainView
}()

& I was getting this same error, so I tried this making it a lazy property:-

lazy private var googleMapsView : GMSMapView = {
    let mainView = GMSMapView()
    mainView.translatesAutoresizingMaskIntoConstraints = false
    mainView.isMyLocationEnabled = true
    return mainView
}()

& voila! It worked like a charm.

Suhail
  • 324
  • 1
  • 15
0

Ensure that the lines you added to AppDelegate.m are actually being executed.

Since the integration with Flipper around react-native v0.62, a portion of the AppDelegate.m file is guarded by an ifdef FB_SONARKIT_ENABLED. If Flipper isn't enabled for whatever reason, any code inbetween the ifdef and the closing endif lines will not be executed. Make sure you paste the #import above the ifdef, and the [GMSServices] line in the didFinishLaunchingWithOptions method, immediately after the opening curly brace ({).

More info on Flipper in React Native: https://fbflipper.com/docs/features/react-native/

Nigh7Sh4de
  • 395
  • 2
  • 7
  • 20
0

Added in the necessery functions providing the API Key in your AppDelegate and still getting this issue? Don't forget to ENABLE the specific API you need (i.e. Maps SDK for iOS/Android) in the Google Cloud Console as well: https://console.cloud.google.com/apis/library

It may take a few minutes for the API to enable/propogate too.

JCutting8
  • 732
  • 9
  • 29