Periodic iOS background location updates

前端 未结 9 1343
佛祖请我去吃肉
佛祖请我去吃肉 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:33
    locationManager.allowsBackgroundLocationUpdates = true
    
    0 讨论(0)
  • 2020-11-22 15:36

    It seems that stopUpdatingLocation is what triggers the background watchdog timer, so I replaced it in didUpdateLocation with:

    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyThreeKilometers];
    [self.locationManager setDistanceFilter:99999];
    

    which appears to effectively power down the GPS. The selector for the background NSTimer then becomes:

    - (void) changeAccuracy {
        [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        [self.locationManager setDistanceFilter:kCLDistanceFilterNone];
    }
    

    All I'm doing is periodically toggling the accuracy to get a high-accuracy coordinate every few minutes and because the locationManager hasn't been stopped, backgroundTimeRemaining stays at its maximum value. This reduced battery consumption from ~10% per hour (with constant kCLLocationAccuracyBest in the background) to ~2% per hour on my device.

    0 讨论(0)
  • 2020-11-22 15:37

    When it is time to start location service and stop background task, background task should be stopped with a delay (1 second should be enough). Otherwise location service wont start. Also Location Service should be left ON for a couple of seconds (e.g. 3 seconds).

    There is a cocoapod APScheduledLocationManager that allows to get background location updates every n seconds with desired location accuracy.

    let manager = APScheduledLocationManager(delegate: self)
    manager.startUpdatingLocation(interval: 170, acceptableLocationAccuracy: 100)
    

    The repository also contains an example app written in Swift 3.

    0 讨论(0)
提交回复
热议问题