Ios CLLocationManager区域监视:检测处于挂起状态的到达
我正在寻找一种方法,可以跟踪用户到达指定坐标集附近的情况。该功能需要在应用程序处于后台时工作(最好在100米以内)。此外,为了保护电池,我理想情况下不希望获得太多的坐标读数(可能每10分钟读取一次,持续时间不超过几个小时) 我尝试了几种方法来完成这项任务,但未能获得预期的结果: 后台计时器: 我在(App.delegate)中添加了一个后台任务 它执行了一个重复计时器。scheduledTimer以获得坐标和进程Ios CLLocationManager区域监视:检测处于挂起状态的到达,ios,swift,background,cllocationmanager,geofencing,Ios,Swift,Background,Cllocationmanager,Geofencing,我正在寻找一种方法,可以跟踪用户到达指定坐标集附近的情况。该功能需要在应用程序处于后台时工作(最好在100米以内)。此外,为了保护电池,我理想情况下不希望获得太多的坐标读数(可能每10分钟读取一次,持续时间不超过几个小时) 我尝试了几种方法来完成这项任务,但未能获得预期的结果: 后台计时器: 我在(App.delegate)中添加了一个后台任务 它执行了一个重复计时器。scheduledTimer以获得坐标和进程 func locationManager(_ manager: CLLocatio
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
检测用户是否在范围内。这种方法在短期内有效,但仅在暂停使用前有效,大约3分钟。理想情况下,我不希望如此频繁地获得协调
区域监控:
我已草签了CLLocationManager,如下所示:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
locationManager.delegate = self
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.activityType = .otherNavigation
locationManager.requestAlwaysAuthorization()
}
当应用程序进入后台时,LocationManager启动:
func applicationDidEnterBackground(_ application: UIApplication) {
self.monitorRegionAtLocation(center: CLLocationCoordinate2D(latitude: x, longitude: y), identifier: id)
locationManager.startUpdatingLocation()
}
区域监测代码:
func monitorRegionAtLocation(center: CLLocationCoordinate2D, identifier: String ) {
// Make sure the app is authorized.
if CLLocationManager.authorizationStatus() == .authorizedAlways {
// Make sure region monitoring is supported.
if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
// Register the region.
let maxDistance = 200.0
let region = CLCircularRegion(center: center,
radius: maxDistance, identifier: identifier)
region.notifyOnEntry = true
region.notifyOnExit = false
locationManager.startMonitoring(for: region)
}
}
}
我为CLLocationManager添加了一个didEnterRegion功能块:
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
if let region = region as? CLCircularRegion {
let identifier = region.identifier
print("FOUND: " + identifier)
}
}
该代码似乎用于检测进入某个区域的情况,但在后台坐标不会更新
其他信息
- 我启用了位置更新和后台提取的后台模式
- 我在Info.plist中提供了“位置始终使用说明”和“使用时位置使用说明”的值
- 应用程序设置显示对该位置的“始终”权限
不幸的是(对于iOS用户来说,幸运的是他们节省了电池),我们没有办法在应用程序被关闭后仅在1小时内获取位置。任何位置更新/监控都需要正确配置其位置管理器,以便它能最好地提供所需的位置更新。在执行后台位置更新时,检查某些点非常重要: 一,。检查位置更新的后台模式,应启用后台提取 二,。应在Info.plist中勾选“位置始终使用说明”和“使用时的位置使用说明” 三,。检查是否要在位置更新之间暂停-如果是,则需要提供活动类型,以便位置经理可以确定暂停位置更新的最佳方式 四,。检查是否要应用距离过滤器-您希望用户(设备)移动一些最小值,以便位置管理器发送更新的位置 五,。检查是否需要所需的精度-这可能会导致某些精度类型的功耗 在您的代码中,我可以看到位置管理器配置了一些参数,但缺少背景模式的精度和距离过滤器
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.activityType = .otherNavigation
此外,如果在其中看到“暂停位置更新”属性,则会显示:
对于具有使用中授权的应用,暂停位置更新
结束对位置更改的访问,直到再次启动应用,然后重试
能够重新启动这些更新。如果您不希望位置更新
完全停止,考虑禁用此属性并更改位置。
当您的应用程序移动到时,精确到KCallocationAccuracyThreekm
背景。这样做允许您继续接收位置
以电源友好的方式进行更新
从本质上讲,它告诉你,如果你想禁用暂停,那么你必须保持精度水平(KCllocationAccuracyThreeKms)。我猜你的方法中没有
此外,您还可以检查哪个实际启动后台任务,然后在后台任务内启动location manager监控。
希望有帮助。谢谢@Paulw11。我已经修改了方法和问题,以更好地适应区域监控,但是我无法从后台获取坐标。您在哪里初始化了
locationManager
?它是局部变量或类属性?要在终止或挂起模式下获取坐标吗?我最近完成了一个关于地理围栏的项目,发现它只有在应用程序处于后台时才起作用,明天我会用我所做的事情来更新你们。如果我能帮助你,我会很高兴的。:)@TarasChernyshenko我目前在AppDelegate中对其进行了初始化。最初设计后台计时器解决方案时,我将其包含在一个单独的BackgroundWorker类中,该类似乎一直工作到应用程序在3分钟后暂停。感谢您的评论。也许我在陈述“背景”时混淆了这个问题,但我的意思是处于暂停状态(我在标题中已经解决了这个问题)。
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.activityType = .otherNavigation