Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background user location when app is terminated/suspended

I've been trying to figure out how to get the user's location when the app is terminated like how the app Moves does it. The only way I know of doing so is with Significant Location Changes. However with significant location changes, the app only gets woken up either every 500 meters and only if it has been around 5 minutes past the last update.

According to Apple:

Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.

In the Moves app, even with the app is terminated, it is able to retrieve the user's location very accurately without having much drainage in battery. It also doesn't seem to have background location turned on as it's reason in the battery usage list only shows "Background Activity". So I'm wondering how an app like Moves is doing this. Any help would be great! Thanks.

like image 328
Zack Avatar asked Sep 10 '25 22:09

Zack


2 Answers

You can register for regions in 500m area and register for notification region notification in them. When it reaches one if them, your app is launched in background. Then, re-register for new ones keeping that location at center.

enter image description here

From Apple's Documentation:

Apps that support background execution may be relaunched by the system to handle incoming events. If an app is terminated for any reason other than the user force quitting it, the system launches the app when one of the following events happens:

For location apps:

  • The system receives a location update that meets the app’s configured criteria for delivery.

  • The device entered or exited a registered region. (Regions can be geographic regions or iBeacon regions.)

like image 52
Sahil Kapoor Avatar answered Sep 12 '25 11:09

Sahil Kapoor


In case you need location updates frequently but still want to save battery life, try this -

  1. Add necessary permissions for using location in background.
  2. Start the updates using startUpdatingLocation
  3. Start deferring updates when in background using allowDeferredLocationUpdatesUntilTraveled:timeout: Specify time and distance filters in this method.
  4. Make sure you read the docs here and specify all the properties correctly to get it working.
like image 37
Shri Avatar answered Sep 12 '25 13:09

Shri