allowDeferredLocationsUpdates working on the simulator but not on my iPhone 6s

筅森魡賤 提交于 2020-02-03 10:47:51

问题


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.

来源:https://stackoverflow.com/questions/39933965/allowdeferredlocationsupdates-working-on-the-simulator-but-not-on-my-iphone-6s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!