Search code examples
iosios7swiftios8cllocationmanager

Significant Location updates after force quit in iOS app


I am currently tracking the user location updates for every mile travelled. And I have background mode turned on for app to look for location updates.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: 

[NSObject: AnyObject]?) -> Bool {
        locationManager.delegate = self

        if iOS8 {
            locationManager.requestAlwaysAuthorization()
        } else {
            locationManager.startUpdatingLocation()
            locationManager.stopUpdatingLocation()
        }

        locationManager.distanceFilter = 1609.34 // meters in 1 mile
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.pausesLocationUpdatesAutomatically = false
        locationManager.startUpdatingLocation()

        return true
    }

But, I am wondering, if the application has been force quit, I would still like for the application to be updating the location. Is that possible with startUpdatingLocation class? Or should I be using startMonitoringSignificantLocationChanges

I read the doc here but didn't quite understand when to move from startUpdatingLocation to startMonitoringSignificantLocationChanges when/while the app is being force quit. Should it be under applicationWillTerminate function ?

Or if that is even possible or is there something else i should be doing.

UPDATE:

I read here

In most cases, the system does not relaunch apps after they are force quit by the user. One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user. In other cases, though, the user must launch the app explicitly or reboot the device before the app can be launched automatically into the background by the system.

If thats the case, should I use startUpdatingLocation or move to startMonitoringSignificantLocationChanges ?


Solution

  • If the app was terminated, you could not run any program for the app.

    And it's not always to handle applicationWillTerminate function when the app was terminated. In some situations, the system kills the app without any notification. Please read the document about application life cycle.

    https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html

    I think you want to do your app is running like a daemon service. iOS does not allow us that unless the device is jail broken.

    https://www.chrisalvares.com/blog/7/creating-an-iphone-daemon-part-1/

    If you are care of the device battery, you would to do like this.

    • startUpdatingLocation and stopMonitoringSignificantLocationChanges at applicationWillEnterForeground.
    • startMonitoringSignificantLocationChanges and stopUpdatingLocation at applicationDidEnterBackground.