菜鸟教程小白 发表于 2022-12-11 18:17:14

ios - 我在哪里设置 Google Maps API key 在操作扩展,iOS Swift


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

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

<p>我试着把它放进去:</p>

<pre><code>override func viewDidLoad() {
            GMSServices.provideAPIKey(&#34;KEY&#34;)
            GMSPlacesClient.provideAPIKey(&#34;KEY&#34;)
            super.viewDidLoad()
    }
</code></pre>

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

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

<pre><code>func locationManager(manager: CLLocationManager, didUpdateLocations locations: ) {
         self.currentCoordinate = Coordinate(longitude: locations.coordinate.longitude, latitude: locations.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)
    }
</code></pre>

<p>我得到的 View 看起来 <a href="/image/6G5ao.png" rel="noreferrer noopener nofollow">like this:</a> </p>

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

<p>我可以验证 <code>GoogleService-Info.plist</code> 在应用运行时具有正确的 APIkey ...</p>

<p>我做错了什么?</p>

<p><strong><em>更新</em></strong></p>

<p>我已验证 'GMSServices.provideAPIKey("KEY")' 和 'GMSPlacesClient.provideAPIKey("KEY")' 均返回 true。因此得出结论,这不是 APIkey 问题。根据这个 <a href="https://code.google.com/p/gmaps-api-issues/issues/detail?id=10862&amp;q=IOS%20maps%20extension&amp;colspec=ID%20Type%20Status%20Introduced%20Fixed%20Summary%20Stars%20ApiType%20Internal" rel="noreferrer noopener nofollow">issue posted on code.google.com</a> 似乎这是一个已知问题</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>聚会有点晚了,但我在今天的扩展中遇到了与 Google Places SDK 类似的问题。尽管成功提供了 APIkey ,但我仍然收到错误消息。 </p>

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

<p>尝试转到 <strong>Google Cloud Platform -> Google Maps -> APIs -> Maps SDK for iOS -> Credentials</strong> 并编辑应用中使用的 key 。在应用程序限制下,您可以将操作扩展的包标识符添加到包标识符列表中。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 我在哪里设置 Google Maps APIkey 在操作扩展,iOS Swift,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40997418/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40997418/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 我在哪里设置 Google Maps API key 在操作扩展,iOS Swift