1

I'm working on an app that monitors the user's location.
After I call startUpdatingLocation() I can put the app in the background. It continues to update the location. That works well.

My question is: can I restart the continuous location updates from a didExitRegion call in the background? Now if I simply call startUpdatingLocation() again, the app gets killed after 10 seconds.

func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
    locationManager.startUpdatingLocation()
}

So the steps would be:

  • I ask the location manager to startUpdatingLocation().
  • The user puts the app in the background while the location updates are continuously being received.
  • The user stops for a while (for a coffee for example) so I ask the location manager to put down a geofence and stopUpdatingLocation().
  • The user continues his drive so when he leaves the geofence I'd like to ask the location manager to startUpdatingLocation() again.
abelorosz
  • 161
  • 12

1 Answers1

3

Please use

startMonitoringSignificantLocationChanges()

which works even after the app is killed. The startUpdatingLocation() is terminated by the system automatically if your app is killed.

You will also need to enable these as indicated in the picture.

Set these setting

Community
  • 1
  • 1
Prateek Varshney
  • 1,114
  • 2
  • 12
  • 29
  • Okay, I can use significant location change monitoring but that won't change that I still need to restart the continuous location updates. – abelorosz Sep 19 '17 at 12:25
  • Can you tell me a use case where you will need that. It is same as startUpdatingLocation except that you will get updates when device travels 400-500 metres and not very short distances. – Prateek Varshney Sep 19 '17 at 12:29
  • This app is a tracker application that provides real-time location information about a user. When he stops for a coffee I'd like to put a geofence around him. When he leaves the geofence I'd like to restart the continuous (real-time) location updates. All in the background. – abelorosz Sep 19 '17 at 12:32
  • As long as you start the significant location updates in the foreground and keep it running when you move to the background then you can start and stop location updates. – Paulw11 Sep 19 '17 at 12:34
  • I am afraid that is still not possible in ios which has been there in android since a long time. Even I was developing a tracking app once, and had to rely on startMonitoringSignificantLocationChanges() since the other one doesnt work in killed state. Though it works like charm in the background state. – Prateek Varshney Sep 19 '17 at 12:35
  • No, you are right. if the user kills the app then iOS assumes that the user wanted it dead, but I don't believe that that is what the OP is asking about. They are just asking about restarting location updates in the background – Paulw11 Sep 19 '17 at 12:38
  • Actually the OP has mentioned about the killed state in the question. – Prateek Varshney Sep 19 '17 at 12:41
  • Yes, I've mentioned the killed state because the app is killed by the OS. And I don't want that. Let's go through this: - I ask the location manager to 'startUpdatingLocation'. - The user puts the app in the background while the location updates are continuously being received. - The user stops for a while so I ask the location manager to put down a geofence and stop the location updates. - The user continues his drive so when he leaves the geofence I'd like to ask the location manager to 'startUpdatingLocation' again. That's it. – abelorosz Sep 19 '17 at 12:42