iosobjective-ccore-locationgeofencing

iOS - CoreLocation and geofencing while app is closed


I've read many tutorials about geofencing my iOS app (i.e., here), but none mention whether or not iOS allows any app-specific location data to be handled when the app is closed.

For instance, I understand that when the app is in the background, these services will still persist (if coded correctly), but how about when the user double taps the home button and closes the app? Can location data still be obtained?


Solution

  • According to the Apple Documentation, in the Using Regions to Monitor Boundary Crossings section:

    In iOS, the regions you register with the location manager persist between launches of your application. If a region crossing occurs while your iOS app is not running, the system automatically wakes it up (or relaunches it) in the background so that it can process the event. When relaunched, all of the regions you configured previously are made available in the monitoredRegions property of any location manager objects you create.

    So yes, your application will be woken up (or relaunched!) when the system's location detects you entered/exited (depending on your setup) a desired region, so this is even if your app isn't running of course. You just need to handle it correctly in the application delegate, when the app is relaunched you get passed a UIApplicationLaunchOptionsLocationKey key in the options dictionary. See documentation link below for details.

    Please remember that the -startMonitoringForRegion:desiredAccuracy: method is deprecated in iOS 6, so it shouldn't be used. Instead use -startMonitoringForRegion.

    To know how to handle when your app is relaunched following a location event, see documentation here, that info as you will see is in the discussion of the deprecated method but it should still be relevant, I believe Apple forgot to migrate this information to the new method when they deprecated the old one. I've filed a bug to them about it.

    UPDATE

    Apple have updated the documentation of CLLocationManager following my bug report. Documentation now specifies for which types of location monitoring the app is or isn't launched after having been terminated. See Using Location Services in the Background