我试图了解deferredLocations功能。根据苹果文档,以下应该可以正常工作,当我在模拟器上运行时,它就会运行,但是一旦我在iPhone 6s上安装应用程序并断开iPhone并运行应用程序,位置更新就不会被推迟。我正在使用XCode 8和swift 3。
以下是代码:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager=CLLocationManager()
locationManager!.delegate=self
locationManager!.desiredAccuracy=kCLLocationAccuracyBest
locationManager!.requestAlwaysAuthorization()
locationManager!.distanceFilter=kCLDistanceFilterNone
locationManager!.allowsBackgroundLocationUpdates=true
locationManager!.pausesLocationUpdatesAutomatically=false
locationManager!.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if (!self.deferringUpdates)
{
locationManager!.allowDeferredLocationUpdates(untilTraveled: CLLocationDistanceMax, timeout: 60)
self.deferringUpdates=true
}
}
func locationManager(_ manager: CLLocationManager, didFinishDeferredUpdatesWithError error: Error?) {
i=i+1
label.text="\(i)"
print("didFinishDeferredUpdatesWithError")
locationManager!.disallowDeferredLocationUpdates()
self.deferringUpdates=false
}
我得到错误代码= 11,这是根据苹果文档不可用的GPS或类似内容。