iOS 版 Google Maps API 的 API 密钥异常 [英] Exception on API Key of Google Maps API for iOS
问题描述
我正在使用适用于 IOS 的 Google Maps API 开发 iOS 应用.我为我的项目安装了 CocoaPod 并根据 Google Developer 上的教程配置它们.但是,当我运行我的项目时,它说
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
*** 由于未捕获的异常GMSServicesException"而终止应用,原因:iOS 版 Google Maps SDK 必须 > 在使用前通过 [GMSServices provideAPIKey:...] 进行初始化"
*** Terminating app due to uncaught exception 'GMSServicesException', reason: 'Google Maps SDK for iOS must > be initialized via [GMSServices provideAPIKey:...] prior to use'
但我已经在 AppDelegate.swift 上调用了GMSServices.provideAPIKey.以下是代码:
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
}
....
(**************) 是我的 API 密钥.
(**************) is my API Key.
并且因为 Google Maps API 使用 Objective C,所以我创建了一个桥接头来导入库.
And because Google Maps API use Objective C, so I created a Bridging Header to import the library.
我试图在 [application:didFinishLaunchingWithOption] 上设置断点.但是在运行该函数之前会引发异常,我认为这很奇怪.
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.
推荐答案
问题终于解决了,原因是我在一个模型类中使用谷歌地图库初始化了一个字段,它会在应用程序运行之前创建.所以这个错误发生了.当我将此变量移动到方法中时,问题解决了.以下是导致错误的代码:
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]{
...
}
}
之后
class PlaceManager {
func getSuggestions(queryString:String) -> [String]{
let placeClient = GMSPlacesClient()
...
}
}
这篇关于iOS 版 Google Maps API 的 API 密钥异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!