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,
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, doesn't it expected to be keep working?
I've never seen a time limit in Apple docs.
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: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:
then you might need to follow the approach of integrating with User Notification Framework - UNLocationNotificationTrigger:
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.
Reduce Accuracy
Set the
desiredAccuracy
property of the location manager objectYou can use one of the
CLLocationAccuracy
constantsAuto-Pause
Set the
pausesLocationUpdatesAutomatically
property of the location manager object totrue
Allow background updates
Set the
allowsBackgroundLocationUpdates
property of the location manager object totrue
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 timeYou can use one of the
CLActivityType
casesDefer Location Update
By the sound of it the app is being killed due to memory constraints. It should however be re-launched when a new location becomes available, as described here: https://developer.apple.com/documentation/uikit/uiapplicationlaunchoptionskey/1623101-location
You should see
application(_:didFinishLaunchingWithOptions:)
being called, and the 'location' key should be present in the launch options. You'll can then re-create whatever is consuming the locations and continue recording.If it's not being re-launched it could be too memory hungry. Check the memory consumption of the app and see if
applicationDidReceiveMemoryWarning(_:)
is being called.I assume you haven't implement background task. You can read here.
In the above link under section "Implementing Long-Running Tasks" point no. 3 is your situation, so it's valid you can use background location update in your project and for same you need to implement a background task too.
.
And below is my ViewController class
You need to do some changes in AppDelegate class ,launch method, applicationDidEnterBackground and applicationDidBecomeActive methods.
Create a sharedInstance for location manager.
FYI please go through this http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended
it might help you.Check comments as well.
Switch to significant location updates when the app moves to background. iOS will unload the app if it keep alive in the background indefinitely.