I’ve been working a project which is about background location update, our target is to achieve permanently updating location in order to collect data.
Background location function does not cost much time for me, but how to make its life stays longer, this question have bothered me for a long time.
Firstly, let’s talk about some basic implementations.
The following link is basically just about the class CLLocationManager that used to update location in IOS. Also, there are steps to teach you how to accomplish a location update function (honestly not very detail, but still enough)
If you want to run it in the background, go Capibilities, open Background Modes and click Location updates.
Don’t forget to request authorization permission.
- Info-plist. (ios 11 requires developer add always and when in usage, always )
Afterward, there are two ways that you can use to update location
- standard location update (service will update location immediately)
startUpdatingLocation() to start the service
stopUpdatingLocation() to stop the service
2. significant distance change (around 500 metres or above)
startMonitoringSignificantLocationChanges() to start the service
stopMonitoringSignificantLocationChanges() to stop the service
Instead of distance range, this method is different from the first one is because the service will automatically wake the app up when new location data arrives no matter app is suspended or terminated. (the first method will survive for certain time and killed by system, but problem can be solved)
Simply add these two lines into your viewDidLoad()
locationManager!.allowsBackgroundLocationUpdates = true
locationManager!.pausesLocationUpdatesAutomatically = false
Yes, it is not complicated at all. Just these two lines, then you can get your GPS update in the background continuously.
After i tested the code, i found that it can only survive not longer than 36 hours. And there are different situations (some can be permanent, some can only last less than 24 hours), it depends on your memory.
That’s what i got from Apple Swift documentation.
“If your app is terminated either by a user or by the system, the system doesn’t automatically restart your app when new location updates arrive. A user must explicitly relaunch your app before the delivery of location updates resumes. The only way to have your app relaunched automatically is to use region monitoring or significant-change location service. However, when a user disables the Background App Refresh setting either globally or specifically for your app, the system doesn’t relaunch your app for any location events, including significant change or region monitoring events. Further, while Background App Refresh is off your app won’t receive significant change or region monitoring events even when it’s in the foreground.”
After one week later, I am 100% sure significant location change can survive over 5 days ( I assume it will last forever ). Unfortunately, there is an disadvantage, you cannot modify its frequency, it can only updates every 500 meters. Therefore, if you aim to stay longer, you have to sacrifice the permission of changing accuracy and distance filter.
But you can switch two methods depending on the situation. For my situation, I use speed to be the changing factor. While you are stationary at a point for certain time, it will automatically change to standard location update.