iOS - 应用关闭时的CoreLocation和地理围栏

Bre*_*ett 43 objective-c core-location ios geofencing

我已经阅读了许多关于地理围栏我的iOS应用程序的教程(例如,这里),但没有提到iOS是否允许在关闭应用程序时处理任何特定于应用程序的位置数据.

例如,据我所知,当应用程序处于后台时,这些服务仍然会持续存在(如果编码正确),但是当用户双击主页按钮并关闭应用程序时呢?还能获得位置数据吗?

Dan*_*iel 67

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

  • 对于任何发现这一点的人来说,不幸的是在iOS7中它不再是真的.如果用户强行通过双击回家退出应用程序,并在多任务中向上滑动,则无法重新启动.引用来自Apple论坛的pmarcos'正如我们在六月份在WWDC的What What New with Multitasking会议中所谈到的那样,iOS 7正在改变.当用户在iOS 7中杀死一个应用程序时,操作系统不会因任何原因重新启动它". (18认同)
  • Marcin,Apple工程师在WWDC上说,因为_all_ apps可以在iOS 7的后台启动,做一些事情,更新他们的快照并再次入睡,对于你明确放弃唤醒的应用程序来说会很奇怪一个小时左右.为了保持一致,用户明确退出的用户现在永远不会在后台被唤醒,如pj4533所述. (2认同)