【问题标题】:Where do i set Google Maps API key in an Action Extension, iOS Swift我在哪里设置 Google Maps API 密钥在操作扩展,iOS Swift
【发布时间】:2016-12-06 14:13:48
【问题描述】:

所以我有这个需要使用谷歌地图的操作扩展。通常当您想在 iOS 应用中使用 Google 地图时,您需要在 AppDelegates didFinishLaunchingWithOptions 函数中添加密钥。

在应用程序扩展中,您没有 AppDelegate 文件来执行此操作,那么我应该把它放在哪里?

我试着把它放进去:

  override func viewDidLoad() {
            GMSServices.provideAPIKey("KEY")
            GMSPlacesClient.provideAPIKey("KEY")
            super.viewDidLoad()
    }

我也尝试将它放在super.viewDidLoad() 之后,但这并没有任何区别。

当我确定我有一个有效的位置时,我会在地图中加载一个视图:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
         self.currentCoordinate = Coordinate(longitude: locations[0].coordinate.longitude, latitude: locations[0].coordinate.latitude)
            let mapCameraPosition:GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(self.currentCoordinate.latitude, longitude: self.currentCoordinate.longitude, zoom: 12)
            let mapFrame:CGRect = CGRect(x: 0, y: 0, width: mapView.frame.width, height: 200)
            let map:GMSMapView = GMSMapView.mapWithFrame(mapFrame, camera: mapCameraPosition)
            self.mapView.addSubview(map)
    }

我得到的视图看起来像like this:

在 Google 控制台中,我为主应用程序和扩展程序添加了捆绑标识符,并将这些标识符的使用限制为 ID。我已经重新生成了 API 密钥并再次添加了它。

我可以验证GoogleService-Info.plist 在应用运行时具有正确的 API 密钥...

我做错了什么?

更新

我已验证 'GMSServices.provideAPIKey("KEY")' 和 'GMSPlacesClient.provideAPIKey("KEY")' 均返回 true。因此得出结论,这不是 API 密钥问题。根据这个issue posted on code.google.com

,这似乎是一个已知问题

【问题讨论】:

  • 您是否在代码中添加了您的谷歌“KEY”?
  • 不,那只是代表我正在使用的密钥。关键不重要。当然,在代码中我使用的是真正的密钥。
  • 为谷歌地图启用详细并再次检查。
  • @New16 你能说得更具体点吗?我不确定我明白你的意思。
  • @Simonk 我对此感到困惑。检查 XCode 设备日志以检查错误。

标签: ios swift google-maps ios-extensions


【解决方案1】:

聚会有点晚了,但我在今天的扩展中遇到了与 Google Places SDK 类似的问题。尽管成功提供了 API 密钥,但我仍然收到错误消息。

我的解决方案是转到 Google Cloud Platform,然后编辑 api-key。我设置了一个限制,只接受来自具有注册包标识符的 iOS 应用程序的请求。我为 Today Extension 添加了丢失的包标识符并保存了。

尝试转到 Google Cloud Platform -> Google Maps -> APIs -> Maps SDK for iOS -> Credentials 并编辑应用中使用的密钥。在应用程序限制下,您可以将操作扩展的包标识符添加到包标识符列表中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    相关资源
    最近更新 更多