OGeek|极客世界-中国程序员成长平台

标题: ios - 我在哪里设置 Google Maps API key 在操作扩展,iOS Swift [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:17
标题: ios - 我在哪里设置 Google Maps API key 在操作扩展,iOS Swift

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

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

我试着把它放进去:

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

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

当我确定我有一个有效的位置时,我会在 map 中加载一个 View :

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

我得到的 View 看起来 like this:

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

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

我做错了什么?

更新

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



Best Answer-推荐答案


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

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

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

关于ios - 我在哪里设置 Google Maps API key 在操作扩展,iOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40997418/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4