Periodic iOS background location updates

前端 未结 9 1444
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 14:56

I\'m writing an application that requires background location updates with high accuracy and low frequency. The solution seems to be a background NSTimer t

9条回答
  •  执笔经年
    2020-11-22 15:28

    1. If you have the UIBackgroundModes in your plist with location key then you don't need to use beginBackgroundTaskWithExpirationHandler method. That's redundant. Also you're using it incorrectly (see here) but that's moot since your plist is set.

    2. With UIBackgroundModes location in the plist the app will continue to run in the background indefinitely only as long as CLLocationManger is running. If you call stopUpdatingLocation while in the background then the app will stop and won't start again.

      Maybe you could call beginBackgroundTaskWithExpirationHandler just before calling stopUpdatingLocation and then after calling startUpdatingLocation you could call the endBackgroundTask to keep it backgrounded while the GPS is stopped, but I've never tried that - it's just an idea.

      Another option (which I haven't tried) is to keep the location manager running while in the background but once you get an accurate location change the desiredAccuracy property to 1000m or higher to allow the GPS chip to get turned off (to save battery). Then 10 minutes later when you need another location update, change the desiredAccuracy back to 100m to turn on the GPS until you get an accurate location, repeat.

    3. When you call startUpdatingLocation on the location manager you must give it time to get a position. You should not immediately call stopUpdatingLocation. We let it run for a maximum of 10 seconds or until we get a non-cached high accuracy location.

    4. You need to filter out cached locations and check the accuracy of the location you get to make sure it meets your minimum required accuracy (see here). The first update you get may be 10 mins or 10 days old. The first accuracy you get may be 3000m.

    5. Consider using the significant location change APIs instead. Once you get the significant change notification, you could start CLLocationManager for a few seconds to get a high accuracy position. I'm not certain, I've never used the significant location change services.

提交回复
热议问题