Location tracking stops after a while when app is in the background Location tracking stops after a while when app is in the background ios ios

Location tracking stops after a while when app is in the background


Switch to significant location updates when the app moves to background. iOS will unload the app if it keep alive in the background indefinitely.

locationManager.pausesLocationUpdatesAutomatically = false


Reduce Accuracy

Set the desiredAccuracy property of the location manager object

self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation

You can use one of the CLLocationAccuracy constants

IMPORTANT

By default, standard location updates on iOS devices run with an accuracy level of best. Change these settings to match your app’s requirements. Otherwise, your app will unnecessarily waste energy.


Auto-Pause

Set the pausesLocationUpdatesAutomatically property of the location manager object to true

self.locationManager.pausesLocationUpdatesAutomatically = true

IMPORTANT

For apps that have in-use authorization, a pause to location updates ends access to location changes until the app is launched again and able to restart those updates. If you do not wish location updates to stop entirely, consider disabling this property and changing location accuracy to kCLLocationAccuracyThreeKilometers when your app moves to the background. Doing so allows you to continue receiving location updates in a power-friendly manner.


Allow background updates

Set the allowsBackgroundLocationUpdates property of the location manager object to true

self.locationManager.allowsBackgroundLocationUpdates = true

Apps that want to receive location updates when suspended must include the UIBackgroundModes key (with the location value) in their app’s Info.plist file and set the value of this property to true. The presence of the UIBackgroundModes key with the location value is required for background updates


Specify an Activity Type

Set the activityType property to let Core Location know what type of location activity your app is performing at a given time

self.locationManager.activityType = .automotiveNavigation 

You can use one of the CLActivityType cases


Defer Location Update

On supported devices with GPS hardware, you can let the location manager defer the delivery of location updates when your app is in the background. For example, a fitness app that tracks the user’s location on a hiking trail can defer updates until the user has moved a certain distance or a certain period of time has elapsed.



After searching for references (talking about any limitation), I assume that Apple Core Location Best Practices video session could be useful! at 06:53 talking about standard location in the background:

furthermore, Core Location won't take any action to ensure your app continues to run, so if you have background run for some reason and you decide to start a location session you might get some updates, but you might also get suspended before you receive all information that you hope to receive...

Actually, I faced this issue before, -as a workaround- the core location was used to keep tracking the location of the user to do unrelated functionality to its location -which is uploading files-, but this workaround didn't work since iOS 9 has been released; I even posted a question referring to this issue.

However, it seems your case is not identical to what I faced, if you are aiming to:

... creates local notification for every time location is updated.

then you might need to follow the approach of integrating with User Notification Framework - UNLocationNotificationTrigger:

The geographic location that the user must reach to enable the delivery of a local notification.

It is also mentioned in the video session (08:59).

Probably, this is could be not what are you looking for, but since we have no guarantee that the background execution will continue running, you might -somehow- find a way to integrate it in your app to achieve the desired functionality.

Update for iOS 11:

You might need to check this answer for the proper way to request the location access.


matomo