iOS - CoreLocation and geofencing while app is closed

南笙酒味 提交于 2019-11-27 16:57:11

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

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)

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!