问题
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?
回答1:
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
回答2:
Some forms of location update require the app to be running in the background, some don't. If you want 'constant' location updates then the app needs to be running in the background. If you want only significant changes (and I think it works for region monitoring too, though the docs aren't quite so explicit) then the app will be relaunched if it was terminated:
If you start this service and your application is subsequently terminated, the system automatically relaunches the application into the background if a new event arrive...
(from CLLocationManager docs)
回答3:
If you use region monitoring capabilities your can resume your app somehow even if is closed, usually by creating a local notification, in this way user can renter in the application and do specific task, by watching the options dictionary in -applicationDidFinishLaunchingWithOptions:
来源:https://stackoverflow.com/questions/17174042/ios-corelocation-and-geofencing-while-app-is-closed