例外:iOS 版 Google Maps SDK 必须在使用前通过 [GMSServices provideAPIKey:...] 进行初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32205872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception:Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use
提问by iBeginner
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:
我正在尝试使用 Swift 2.0 在我的项目中实现 Google Maps SDK。我按照这个但是在运行这个时,我的应用程序收到以下错误:
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.
我已经尝试了 StackOverflow 中所有可能的解决方案。
回答by Rajasekaran Gopal
You need to set this both in didFinishLaunchingWithOptions
您需要在 didFinishLaunchingWithOptions 中进行设置
GMSPlacesClient.provideAPIKey("Your key")
GMSServices.provideAPIKey("Your key")
回答by Jasveer
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.
我只是遇到了同样的问题。我创建了一个 GMSMapView 对象,它在可能可以读取 api 密钥之前被初始化。所以我将它移到 viewDidLoad 方法中,问题解决了。
Before :
前 :
class ViewController: ..... {
let mapView = GMSMapView()
After :
后 :
class ViewController: ..... {
var mapView : GMSMapView?
override viewDidLoad(){
mapView = GMSMapView()
回答by Vu Tran
Just adding to Rajasekaran Gopal
只是添加到 Rajasekaran Gopal
Remember to add
记得添加
import GoogleMaps
import GooglePlaces
then
然后
GMSPlacesClient.provideAPIKey("Your key")
GMSServices.provideAPIKey("Your key")
in didFinishLaunchingWithOptions
在 didFinishLaunchingWithOptions 中
回答by emotality
Here is a Google Maps tutorial for Swift:
这是 Swift 的 Google 地图教程:
http://www.appcoda.com/google-maps-api-tutorial/
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:
- In the sidebar on the left, select Credentials.
- If your project doesn't already have an iOS API key, create one now by selecting Add credentials > API key > iOS key.
- In the resulting dialog, enter your app's bundle identifier. For example:
com.example.hellomap
.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
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"];
第 5 步:获取 iOS API 密钥
使用 API 密钥可让您监控应用程序的 API 使用情况,并确保 Google 可以在必要时就您的应用程序与您联系。该密钥是免费的,您可以将其用于任何调用 iOS 版 Google Maps SDK 的应用程序,并且它支持无限数量的用户。您可以通过提供应用程序的包标识符从 Google Developers Console 获取 API 密钥。
如果您的项目还没有 iOS 应用程序的密钥,请按照以下步骤从 Google Developers Console 创建 API 密钥:
- 在左侧的边栏中,选择Credentials。
- 如果您的项目还没有 iOS API 密钥,请立即通过选择添加凭据 > API 密钥 > iOS 密钥来创建一个。
- 在出现的对话框中,输入您的应用程序包标识符。例如:
com.example.hellomap
。单击创建。
您的新 iOS API 密钥出现在您项目的 API 密钥列表中。API 密钥是一串字符,如下所示:
AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0
将您的 API 密钥添加到您
AppDelegate.m
的如下:
- 添加以下导入语句:
@import GoogleMaps;
- 将以下内容添加到您的应用程序:didFinishLaunchingWithOptions: 方法,将 API_KEY 替换为您的 API 密钥:
[GMSServices provideAPIKey:@"API_KEY"];
回答by Pavel Bondar
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
.
如果您已经GMSMapView
在界面构建器中将类设置为您的视图,请GMSServices.provideAPIKey("API key")
在包含GMSMapView
.
回答by iOS
In my app i provided key to GMSPlacesClient in AppDelegate -didFinishLaunchingWithOptions
在我的应用程序中,我在 AppDelegate -didFinishLaunchingWithOptions 中提供了 GMSPlacesClient 的密钥
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")
回答by popic.m
You should set up API keys before you set up the UIWindow in AppDelgate, if your initial View Controller uses GoogleMaps.
如果您的初始视图控制器使用 GoogleMaps,您应该在 AppDelgate 中设置 UIWindow 之前设置 API 密钥。
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
}
回答by iman kazemayni
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)
在我的情况下 --didFinishLaunchingWithOptions-- 方法没有调用,因为我更新了 swift 版本。首先确保此方法正常调用。就我而言,我不需要 GMSPlacesClient.provideAPIKey("Your key")。但是,您可以在初始视图控制器中初始化 GMSServices(如果它不包含地图或与之相关的对象)
回答by Hemant Sharma
You are skipping provideAPIKey in your AppDelegate check project
您正在 AppDelegate 检查项目中跳过 provideAPIKey
Your_PROJECT -> ios -> Runner -> AppDelegate.m
Your_PROJECT -> ios -> Runner -> AppDelegate.m
Replace your file code with below snippet add your APIKEY
用下面的代码片段替换您的文件代码添加您的 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:...]
iOS 版 Google Maps SDK 必须通过 [GMSServices provideAPIKey:...] 进行初始化