iOS-应用程式关闭时的CoreLocation和地理围栏 [英] iOS - CoreLocation and geofencing while app is closed

查看:108
本文介绍了iOS-应用程式关闭时的CoreLocation和地理围栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了许多有关对我的iOS应用程序进行地理栅栏设置的教程(例如,此处),但没有提及iOS在关闭应用程序后是否允许处理特定于应用程序的位置数据.

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?

推荐答案

根据

在iOS中,您在位置管理器中注册的区域仍然存在 在应用程序启动之间. 如果发生区域交叉 当您的iOS应用未运行时,系统会自动将其唤醒 在后台重新启动(或重新启动),以便它可以处理 活动.重新启动后,您先前配置的所有区域 可在任何位置的MonitoredRegions属性中使用 您创建的管理员对象.

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.

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

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.

请记住,-startMonitoringForRegion:desiredAccuracy:方法在iOS 6中已弃用,因此不应使用.而是使用-startMonitoringForRegion.

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.

更新

Apple已在我的错误报告之后更新了CLLocationManager的文档.现在,文档说明了终止后针对应用程序启动或不启动的位置监控的类型. 请参阅使用位置服务在后台

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

这篇关于iOS-应用程式关闭时的CoreLocation和地理围栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆