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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17174042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:58:59  来源:igfitidea点击:

iOS - CoreLocation and geofencing while app is closed

iosobjective-ccore-locationgeofencing

提问by Brett

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.

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

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?

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

回答by Daniel

According to the Apple Documentation, in the Using Regions to Monitor Boundary Crossingssection:

根据Apple 文档,在Using Regions to Monitor Boundary Crossings部分中:

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.

在 iOS 中,您在位置管理器中注册的区域在您的应用程序启动之间保持不变。如果在您的 iOS 应用程序未运行时发生区域交叉,系统会在后台自动唤醒(或重新启动)它,以便它可以处理该事件。重新启动后,您之前配置的所有区域都可以在您创建的任何位置管理器对象的 monitoringRegions 属性中使用。

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 UIApplicationLaunchOptionsLocationKeykey in the options dictionary. See documentation link below for details.

所以是的,当系统的位置检测到您进入/退出(取决于您的设置)所需区域时,您的应用程序将被唤醒(或重新启动!),因此即使您的应用程序没有运行当然也是如此。您只需要在应用程序委托中正确处理它,当应用程序重新启动时,您会UIApplicationLaunchOptionsLocationKey在选项字典中传递一个键。有关详细信息,请参阅下面的文档链接。

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

请记住,该-startMonitoringForRegion:desiredAccuracy:方法在 iOS 6 中已弃用,因此不应使用。而是使用-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.

要了解在位置事件后重新启动您的应用程序时如何处理,请参阅此处的文档,您将看到的信息在已弃用方法的讨论中,但它应该仍然相关,我相信 Apple 忘记将此信息迁移到当他们弃用旧方法时的新方法。我已经向他们提交了一个关于它的错误。

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

苹果根据我的错误报告更新了 CLLocationManager 的文档。文档现在指定应用程序在终止后启动或不启动的位置监控类型。请参阅在后台使用定位服务

回答by Wain

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)

(来自CLLocationManager 文档

回答by Andrea

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:

如果您使用区域监控功能,即使关闭,您也可以以某种方式恢复您的应用程序,通常是通过创建本地通知,这样用户可以通过查看选项字典来租用应用程序并执行特定任务 -applicationDidFinishLaunchingWithOptions: