WatchOS2

时间:2015-11-17 09:11:09

标签: xcode swift mapkit watchkit apple-watch

我正在尝试在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])我运行此代码:

    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)

未被调用

1 个答案:

答案 0 :(得分:0)

我找到了一个解决我问题的库。我不知道究竟发生了什么,但它现在有效。

我使用了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
    }
}

在信用到期时给予信用 -