请求用户的当前位置观看OS2

时间:2015-10-09 02:17:17

标签: ios cllocationmanager apple-watch cllocation watch-os-2

我正在尝试修复用户当前位置,并在地图上显示一些数据。我的手表套件扩展名的代码如下。

   import Foundation
    import CoreLocation

    class InterfaceController: WKInterfaceController, CLLocationManagerDelegate {

        override func awakeWithContext(context: AnyObject?) {
            super.awakeWithContext(context)
            // Configure interface objects here.
        }

        override func willActivate() {
            // This method is called when watch view controller is about to be visible to user
            super.willActivate()
        }

        override func didDeactivate() {
            // This method is called when watch view controller is no longer visible
            super.didDeactivate()
        }



        func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
          dLog("Did get a location.")
        }

        func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
            dLog("Did fail to retrieve a location.")
        }

        func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
            print(status)
        }

        @IBAction func btnRequestLocation() {
            let locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestLocation()
        }

我还确保在WatchKit扩展目标链接框架和库下包含CoreLocation框架。但是,在手表模拟器和与iOS应用程序配对的真正Apple手表上运行此代码,这些委托回调永远不会被解雇。我不确定我是否错过了一个阻碍我获取位置的地方。我已经尝试重置位置和隐私,我甚至得到了我的应用程序需要位置权限的请求。我点击“允许”但没有任何位置被发送到Apple Watch扩展程序。任何帮助将不胜感激。

编辑:

我还为iOS 9采取了一些额外的步骤。

项目 - > iOS目标 - >背景模式 - 检查位置更新 - iOS Target还包含plist文件中的“Required Background Modes”键。

以下是iOS App的属性列表。

enter image description here

更新 似乎这可能是手表OS2中孤立的常见问题。 https://forums.developer.apple.com/thread/14828

1 个答案:

答案 0 :(得分:0)

您正在请求requestAlwaysAuthorization,因此您需要确保已启用地点背景模式并设置allowsBackgroundLocationUpdates=TRUE(适用于iOS9)。

但是,我建议您申请requestWhenInUseAuthorization,因为您只是要求通过requestLocation()进行一次性的位置更新。

相关问题