Getting Location for an iOS App When It Is in the Background and Even Killed

iOS 13 track user location when app is killed

You have 2 options to track user location when app is killed:

1 -> Region Monitoring aka geofencing: You will setup a region to be monitored & when user enter or leave that region, the iOS system will wake up your app & notify you in application delegate about location update.
https://developer.apple.com/documentation/corelocation/monitoring_the_user_s_proximity_to_geographic_regions

2-> Significant-Change location service: In this case, iOS system will wake up your app only when user's location is significantly changed. The value is around 500 meters.
https://developer.apple.com/documentation/corelocation/getting_the_user_s_location/using_the_significant-change_location_service

Note: For both of these features to work, you will need 'Always' location permission from user.

Decided which method is suitable for you & then dive into the its documentation.

Getting location for an iOS app when it is in the background and even killed

To get a location in the background, use the following code. It will make your app run in the background for a long time by restarting the background task everytime.

To use this, you need to turn on Background Mode in Capabilities in project settings with Background Fetch and Location Updates turned on.

- (void)applicationDidEnterBackground:(UIApplication *)application {

if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4

if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance

__block UIBackgroundTaskIdentifier background_task; //Create a task object

background_task = [application beginBackgroundTaskWithExpirationHandler: ^{
[application endBackgroundTask:background_task]; //Tell the system that we are done with the tasks
background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
//System will be shutting down the app at any point in time now
}];
}
}
}

How continue doing iOS location tracking even after the app is killed?

It seems WhatsApp could be using PushKit to get around this problem. According to this question:

WhatsApp could be using VOIP background mode along with PushKit for
solving this problem.

Voip pushes are:

  • delivered directly to the app.
  • considered high-priority notifications and are delivered without delay.
  • delivered even if the app was force-quit by the user.

I have also created a quick sample app to try this and it works like a charm. However, again according to the question linked, Apple doesn't really allow this solution:

Apple no longer allows the usage of the API for push notifications of non-VOIP apps. They do however allow WhatsApp to do it in their infinite fairness.

IOS Getting location updates when app terminated without using significantChange

Looking at the below content from the Apple doc, you clearly have 2 alternatives against using significant location changes to wake a app from the background. I have marked in bold the services you can use to relaunch the app if it has been terminated.

Using Location Services in the Background Most location services are
meant to be used while your app is in the foreground but some can also
run in the background. In some cases, location events can even cause
your app to be relaunched to process an event. To run most location
services in the background, you need to enable the location updates
background mode in the Capabilities tab of your Xcode project. For
services that launch your app, you need to request (and be granted)
“Always” authorization from the user.

The standard location service delivers events normally while an app is
running in the foreground. When your app is in the background, this
service delivers events only when the location-updates background mode
is enabled for the app. This service does not relaunch iOS apps that
have been terminated.

The significant location change service delivers events normally
while an app is running in the foreground or background. For a
terminated iOS app, this service relaunches the app to deliver
events.
Use of this service requires “Always” authorization from the
user.

The region monitoring service delivers events normally while an
app is running in the foreground or background. (You can use this
service for both geographic and beacon regions.) For a terminated
iOS app, this service relaunches the app to deliver events.
Use of
this service requires “Always” authorization from the user.

Beacon ranging delivers events normally while an app is running in the
foreground. When your app is in the background, this service delivers
events only when the location-updates background mode is enabled for
the app and the standard location service is running. (If the beacon
region’s notifyEntryStateOnDisplay property is YES, waking the device
causes the app to range for beacons for a few seconds in the
background.) This service does not relaunch iOS apps that have been
terminated; however, you can be relaunched by monitoring beacon
regions using the region monitoring service.

The heading service delivers events normally while an iOS app is
running in the foreground. When your app is in the background, this
service delivers events only when the location-updates background mode
is enabled for the app and the standard location service is running.
This service does not relaunch iOS apps that have been terminated.

The visit service delivers events normally while an iOS app is
running in the foreground. When your app is in the background, this
service delivers events only when the location-updates background mode
is enabled for the app and the standard location service is running.
For a terminated iOS app, this service relaunches the app to deliver events. Use of this service requires “Always” authorization from the
user.

Enabling the location-updates background mode ensures that an app
continues to receive location events while in the background. When the
app moves to the background, the system adds the location-services
indicator to the status bar to let the user know that an app is using
location services. The system may still terminate the app at any time
to reclaim its memory or other resources.

Also from the doc,

Getting the Visited Locations
In iOS, the visits service provides an alternative to the significant location change service for apps that need location
information about interesting places that the user visited.
For
example, if the user is in one location for an extended period of
time, the service might generate an event when the user arrives at
that location and another when the user leaves that location. The
service is intended for apps that might already be using the
significant location change service and want an even lower power way
to do so. You would not use this service to create navigation apps or
apps that rely on regular location updates.

Document Link:

https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/doc/uid/TP40007125-CH3-SW73

How Can I Continue updating my location even when app is killed or removed from background in ios?

Its possible, but you'll have to jump through a few hoops.
The only way to send location updates when killed is by using Region Monitoring (https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html).
When setup up, your app would be opened by the OS then you have a few seconds to process information before the app is killed. Its enough time to send location updates to your server/local storage.
There is also another API called CLVisit, however, its completely controlled by the operating system and not reliable.



Related Topics



Leave a reply



Submit