【问题标题】:How to properly provide api key for GoogleMaps?如何为 GoogleMaps 正确提供 api 密钥?
【发布时间】:2020-09-24 13:41:28
【问题描述】:

目前,AppDelegate 调用一个静态方法,为 GMSServices 提供 API 密钥。密钥正确,方法本身返回true。

GMSServices.provideAPIKey(ConfigUtils.GoogleMaps.Key)

尽管如此,当我尝试打开地图时,出现错误

Exception NSException * "Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use" 0x000060000383c8d0 发生在应用程序中。

我还需要做什么才能使 GoogleMaps 正常工作吗?

我在另一个问题中发现了这个:

如果您的某些 Google 地图代码在 API 密钥初始化之前运行,则可能会发生这种情况。

但在 API 密钥初始化之前没有任何 Google 地图代码。

【问题讨论】:

  • 你能展示你的 AppDelegate 代码吗?

标签: ios swift google-maps google-maps-sdk-ios


【解决方案1】:

让 Google Maps API 在您的应用中运行应该不需要其他任何东西。只要对您的 API 密钥的引用是正确的,那么可能是您在 AppDelegate 中的错误位置调用了 provideAPIKey()

您确定在 AppDelegate 中的正确函数中调用 provideAPIKey() 吗?它应该在didFinishLaunchingWithOptions 函数中调用,就像这样。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    // Provide the API Key to allow the app to use Google Maps here.
    GMSServices.provideAPIKey(ConfigUtils.GoogleMaps.Key)
        
    return true
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-14
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2015-11-23
    相关资源
    最近更新 更多