allowDeferredLocationsUpdates working on the simul

2019-07-04 03:21发布

问题:

I'm trying to understand the deferredLocations feature. As per apple docs, the following should work, and it does when I run it on the simulator, but once I install the app on my iPhone 6s and disconnect the iPhone and run the app, the locations updates do not get deferred. I'm using XCode 8 and swift 3.

Here is the code:

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
}

I'm getting error code = 11, which is GPS not available or something like that as per apple docs.