gpt4 book ai didi

ios - 终止/暂停时显着更改位置 API 的行为?

转载 作者:IT王子 更新时间:2023-10-29 07:28:43 24 4
gpt4 key购买 nike

这是来自 CLLocationManager 的部分使用 startMonitoringSignificantLocationChanges 描述应用程序行为的文档:

If you start this service and your application is subsequently terminated, the system automatically relaunches the application into the background if a new event arrives. In such a case, the options dictionary passed to the application:didFinishLaunchingWithOptions: method of your application delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your application was launched because of a location event. Upon relaunch, you must still configure a location manager object and call this method to continue receiving location events. When you restart location services, the current event is delivered to your delegate immediately. In addition, the location property of your location manager object is populated with the most recent location object even before you start location services.

所以我的理解是,如果您的应用程序终止(我假设如果您不从 stopMonitoringSignificantLocationChanges 调用 applicationWillTerminate ),您将被 UIApplicationLaunchOptionsLocationKey 唤醒参数 application:didFinishLaunchingWithOptions .那时你创建你的 CLLocationManager , 调用 startMonitoringSignificantLocationChanges并为 limited time 进行后台定位处理.所以我对这一点没意见。

上一段只谈了应用程序终止时会发生什么,并没有建议您在应用程序挂起时做什么。 didFinishLaunchingWithOptions 的文档说:

The application tracks location updates in the background, was purged, and has now been relaunched. In this case, the dictionary contains a key indicating that the application was relaunched because of a new location event.

建议您只会在您的应用程序启动时(因为位置更改)在您被终止后收到此电话。

不过关于Significant Change Service的段落在Location Awareness Programming Guide有以下要说的:

If you leave this service running and your application is subsequently suspended or terminated, the service automatically wakes up your application when new location data arrives. At wake-up time, your application is put into the background and given a small amount of time to process the location data. Because your application is in the background, it should do minimal work and avoid any tasks (such as querying the network) that might prevent it from returning before the allocated time expires. If it does not, your application may be terminated.

这表明如果您的应用已暂停,您将通过位置数据被唤醒,但未提及您是如何被唤醒的:

在写这篇文章的过程中,我想我可能刚刚回答了我自己的问题,但如果我对此的理解得到更有知识的人的证实,那就太好了。

最佳答案

自从我问了这个问题后,我做了一些测试(主要是在家和公司之间的火车上),并确认暂停应用程序的行为正如我在问题末尾所怀疑的那样。

也就是说,您暂停的应用程序被唤醒,您不会在应用程序委托(delegate)上收到任何回调,而是通过现有的 CLLocationManagerDelegate 接收您的位置更新。 .您可以通过检查 applicationState 来检测您是否在后台运行,并在您从挂起状态唤醒以进行位置处理的情况下做有限的工作。

[UIApplication sharedApplication].applicationState == UIApplicationStateBackground

我通过位置测试工具得出了这个结论,欢迎您使用 download并尝试一下。这是一个非常简单的应用程序,允许您通过 UI 打开重大更改和 GPS 更改 API,并记录您返回的所有响应。

N.B. 上一个答案中的第六点不正确。 冻干暂停的应用程序在从暂停状态唤醒时会收到 CLLocationManagerDelegate 回调。

关于ios - 终止/暂停时显着更改位置 API 的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3421242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com