我在swift中试过这个
How to get the current location in watchOS 2?
但它没有用 这是我在WKInterfaceController
中的代码override func awake(withContext context: Any?) {
super.awake(withContext: context)
self.setupLocation()
}
extension InterfaceController: CLLocationManagerDelegate {
func setupLocation() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let currentLocation = locations.first else { return }
self.currentLocation = currentLocation
let location = "lat: \(self.currentLocation.coordinate.latitude)\nlong: \(self.currentLocation.coordinate.longitude)"
self.locationLabel.setText(location)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("LOCATION MANAGER DID FAIL ERROR : \(error)")
}
}
我已经在 WatchExtension 和主要应用 info.plist中添加了权限。
它仍然没有要求许可警报。 我错过了什么???? :(