Background user location when app is terminated/suspended

爱⌒轻易说出口 提交于 2019-12-17 18:26:46

问题


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.


回答1:


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.

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.)




回答2:


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.



回答3:


The only solution is to use background mode with CoreLocation. If your app requires iOS 8, the system will relaunch your app, even if force quit by the user. This is documented in Apple Docs:

In most cases, the system does not relaunch apps after they are force quit by the user. One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user. In other cases, though, the user must launch the app explicitly or reboot the device before the app can be launched automatically into the background by the system.




回答4:


in iOS 8 they are new steps that we need to pay attention to in order make make the location fetching work. The first step is to add either one or two keys into the project’s .plist depending on the main functionality of the app. The two keys are NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription, you will then need to add a String that explains to the user why does the app needs to access his location, something among the lines of “This app uses location in the background/foreground because of A, B and C”. Each of these Strings has a corresponding authorization method that needs to be called, WhenInUse or Alway (i.e. Background).

- (void)startStandardUpdates
{
    // Create the location manager if this object does not
    // already have one.
    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

    // Set a movement threshold for new events.
    locationManager.distanceFilter = 500; // meters

    [locationManager startUpdatingLocation];
}



回答5:


From my current experience, you have 3 options to make this work. The first one is to have a silent notification which requires an effort done by the backend. The silent notification is some sort of a push notification but it notifies your app without an alert. The second solution is background fetches. Background fetches don't require a backend solution but sadly you can't control the intervals needed to instantiate it. The third one is having a scheduled local notifications with no messages.




回答6:


From the docs

Tracking the User’s Location

There are several ways to track the user’s location in the background, most of which do not actually require your app to run continuously in the background:

The significant-change location service (Recommended)

Foreground-only location services

Background location services

Background Execution

And for the details - Getting the User’s Location



来源:https://stackoverflow.com/questions/28790893/background-user-location-when-app-is-terminated-suspended

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