Exception on API Key of Google Maps API for iOS
Asked Answered
W

4

6

I am developing an iOS app using Google Maps API for IOS. And I installed the CocoaPod for my project and configure them according to tutorial on Google Developer. However, when I run my project, it says

*** Terminating app due to uncaught exception 'GMSServicesException', reason: 'Google Maps SDK for iOS must > be initialized via [GMSServices provideAPIKey:...] prior to use'

But I already call "GMSServices.provideAPIKey on the AppDelegate.swift. Following is the code:

....
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    GMSServices.provideAPIKey("***********************")
    return true
}
....

(**************) is my API Key.

And because Google Maps API use Objective C, so I created a Bridging Header to import the library.

I tried to set breakpoint on [application:didFinishLaunchingWithOption]. But it will raise exception before run that function, which I think is very weird.

So confused about it. Thanks in advance.

Wernick answered 27/9, 2015 at 20:48 Comment(1)
The exception happen only if you didnt include GMSServices.provideAPIKey() in your didFinishLaunchingWithOptions method, you should paste all the lines from your AppDelegate file.Tranquilizer
W
5

Problem finally solved, the reason is that I initialize a fields using Google Maps library in the one model class and it will be created before the app run. So this error happens. When I moved this variable into the method, problem solved. Following is the code that causes error:

class PlaceManager {
    let placeClient = GMSPlacesClient()
    ...
    func getSuggestions(queryString:String) -> [String]{
        ...
    }
}

After

class PlaceManager {
    func getSuggestions(queryString:String) -> [String]{
        let placeClient = GMSPlacesClient()
        ...
    }
}
Wernick answered 29/9, 2015 at 1:13 Comment(0)
H
3

Instead of didFinishLaunchingWithOptions, move the call to willFinishLaunchingWithOptions. This method is called after state restoration has occurred but before your app’s window and other UI have been presented. (Which in your case might be a UI that consumes Google Map API)

func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
GMSServices.provideAPIKey("***********************")

    return true
}
Hebephrenia answered 28/9, 2015 at 10:17 Comment(0)
A
2

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()
Amu answered 3/3, 2017 at 22:16 Comment(2)
This is the same response that someone else already gave. Consider upvoting that answer rather than adding your own.Uppsala
This is it. This solved my problem when I already have an API key attached to didFinishLaunchingWithOptionsMuzzle
F
0

In your appDelegate.m at didFinishLaunchingWithOptions this add these GMSService line with api key for me it worked.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [GMSServices provideAPIKey:@"**************"];
  [FIRApp configure];
}

and add this import at the top

#import <GoogleMaps/GoogleMaps.h>
Forbear answered 28/11, 2023 at 9:23 Comment(1)
I don't think this addresses the original 'GMSServicesException'.Tem

© 2022 - 2025 — McMap. All rights reserved.