WatchOS2 上的位置始终为零

Location always nil on WatchOS2

我正在尝试在我的 WatchOS2 应用程序上检索我当前位置的 de Lat Long。 该应用程序已通过身份验证,iPhone 应用程序运行正常。
info.plist: NSLocationWhenInUseUsageDescription 已设置。

我在willActivate()

中使用了下面的代码
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        if   (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse)
        {
            locationManager.requestWhenInUseAuthorization()
            print("Logon: Location not authorized1")
        }

    }   

        locationManager.requestLocation()

locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])i 运行这段代码中:

    if locationManager.location != nil
    {
        self.userLat = String(self.locationManager.location!.coordinate.latitude)
        self.userLong = String(self.locationManager.location!.coordinate.longitude)

        print("Lat\(userLat) Long \(userLong)")
    }
    else
    {
        print("An error occurred") <- It always prints this error
    }
}


locationManager(manager: CLLocationManager, didFailWithError error: NSError)

没有被调用

我找到了一个解决我问题的库。我不知道到底发生了什么,但现在可以了。

我用了OneShotLocationManger by icanzilb

我知道你想在你的 WatchOS2 应用程序中使用它,你必须将 startUpdatingLocation() 更改为 requestLocation(),因为 WatchOS 不允许继续更新。
像这样:

    //location authorization status changed
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

    switch status {
    case .AuthorizedWhenInUse:
        self.locationManager!.requestLocation()
    case .Denied:
        _didComplete(nil, error: NSError(domain: self.classForCoder.description(),
            code: OneShotLocationManagerErrors.AuthorizationDenied.rawValue,
            userInfo: nil))
    default:
        break
    }
}

在信用到期时给予信用 -