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

Okan Kocyigit picture Okan Kocyigit · Aug 11, 2017 · Viewed 15.1k times · Source

I've created a simple app which tracks user location and creates local notification for every time location is updated.

I enabled the background modes below,

enter image description here

let locationManager = CLLocationManager()

open override func viewDidLoad() {
       locationManager.delegate = self;
       locationManager.desiredAccuracy = kCLLocationAccuracyBest;
       locationManager.distanceFilter = 10
       locationManager.allowsBackgroundLocationUpdates = true
       locationManager.startUpdatingLocation()
}

open func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
       let notification = UILocalNotification()
       notification.alertBody = "location updated"
       notification.fireDate = Date()
       UIApplication.shared.scheduleLocalNotification(notification)
}

I set string for NSLocationAlwaysUsageDescription and ask for permission. User grant permission for always usage when the app loaded first time.


It's working well when app is in the foreground, when it goes background still working at least in 5-40 minutes time range which is changeable by battery or other opened apps.

The problem is why it stops working, Isn't it expected to be keep working?

I've never seen a time limit in Apple docs.

Answer

Vimal Saifudin picture Vimal Saifudin · Aug 14, 2017

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