Exception on API Key of Google Maps API for iOS

南笙酒味 提交于 2019-12-19 19:38:25

问题


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.


回答1:


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()
        ...
    }
}



回答2:


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
}



回答3:


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()


来源:https://stackoverflow.com/questions/32812685/exception-on-api-key-of-google-maps-api-for-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!