3

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.

the_ccalderon
  • 2,006
  • 2
  • 13
  • 24
  • Well, maybe the GPS _isn't_ available. Did you check your device settings? You are asking for authorization but I don't see you checking to see whether you actually have it, or even whether location services are turned on. – matt Oct 08 '16 at 15:22
  • How can I check these? I thought by asking for authorization I was actually getting access to the GPS and the location services. – the_ccalderon Oct 08 '16 at 15:27
  • Okay, I take it all back. I think you've found a bug. See this report: https://github.com/lionheart/openradar-mirror/issues/15939 That is the same as what's happening to you. I see it too; `CLLocationManager.deferredLocationUpdatesAvailable` always returns `false`. – matt Oct 08 '16 at 16:21
  • Okay, I take back the takeback. It's not a bug. It's because you are still attached to the computer. http://stackoverflow.com/a/26345001/341994 – matt Oct 08 '16 at 16:29
  • Nope, I already read that and I'm always running the app with the device disconnected. – the_ccalderon Oct 08 '16 at 16:39
  • Yup, I take back the takeback of the takeback. I wrote a test app that runs disconnected from the computer and I can never get `CLLocationManager.deferredLocationUpdatesAvailable` to be `true`. – matt Oct 08 '16 at 16:57
  • So it's a bug! Thank you very much matt. Let's see when they fix it. – the_ccalderon Oct 08 '16 at 16:58
  • I can definitely confirm that this is working differently on iOS 10 than iOS 9. In iOS 9 CLLocationManager.deferredLocationUpdatesAvailable is `true`. In iOS 10 it isn't. There are other issues here, but that's a clear indication that something's wrong. – matt Oct 08 '16 at 17:56

0 Answers0