4

My application need to post the location data to my backend. I have set the location permissions to always and set the pausesLocationUpdatesAutomatically to false for CLLocationManager and need to continue tracking the phones location even when the application is in background or after the phone is restarted.

I'm able to make it work when the application is in background mode. But it stops working when the phone is restarted.

How can I do this?

By the way, I know the

Bagusflyer
  • 12,675
  • 21
  • 96
  • 179
  • 1
    Good question :D – Fogmeister Aug 24 '18 at 09:32
  • Actually I come out another question. That is how can I know the device is rebooted when my application is in background. – Bagusflyer Aug 26 '18 at 14:09
  • Based on my testing, the `application didLaunched` event has not been called after reboot if the passcode is set. The event will only be fired after the device has been unlocked. Is there any workaround for this? – Bagusflyer Sep 05 '18 at 03:39

1 Answers1

1

This is WELL documented by Apple documentations. Only if Location manager is started with "startMonitoringSignificantLocationChanges" you can get it.

So for example:

1) in App delegate start a singleton GeoLocationManager.shared.locationManager, so if you restart will be running.

2) in call back:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
{

update your position

it will be re-entrant if crashes. I think You already set il plist:

<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array>

note: add code for privacy, as since ios9 is mandatory..

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
...
}

And in plist:

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Needed to track on maps even if background</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Needed to track on maps even if background</string>
ingconti
  • 10,876
  • 3
  • 61
  • 48
  • Thanks. It works for me to monitor the location tracking when the application is in background. I use some tricks to keep the background job running forever. But it stops after the devices reboot. Maybe my question is more relevant to how to keep the background job running forever even after reboot. – Bagusflyer Aug 24 '18 at 13:07
  • @ZhouHao you don't (you can't) keep the job running forever. iOS monitors the location. If you ask for updates then you will receive them from iOS when it wants to tell you about them. You can't keep a task running forever. There are quite a few tutorials for doing this. Most of them are to do with creating exercise apps etc... – Fogmeister Aug 24 '18 at 15:10
  • @Fogmeister Thanks for your reply. Actually I can keep the background job running forever until the device reboot. This is done by a trick which is not allowed by Apple store. Since my app is an enterprise app so it won't affect me. I understand the suggestion which will update the location if I monitor the significant location change (500m). But this is not what I want. I want to track the location more accurately and post data to backend. – Bagusflyer Aug 25 '18 at 02:32
  • @ingconti The application won't be launched automatically even I startMonitoringSignificantLocationChanges if the passcode is set. Even after I move long distance, the application will not be launched automatically as mentioned in Apple document. Have to launch the application manually. – Bagusflyer Sep 07 '18 at 02:40
  • @Zhou Hao I can make other tests, but on iOS9 worked so. it seems strange. How you detect it does not track? I tested logging on disk and reading logs via "its file sharing" on mac.(Logging opens/writes/closes the log file). Invoking network can b misleading for initial testing. – ingconti Sep 07 '18 at 05:59
  • @ingconti I'm working on iOS 10 and iOS 11. I add a log file and also an audio sound for debugging. I noticed the application only launched after I key in the passcode and select my application. – Bagusflyer Sep 07 '18 at 08:29