ios - iBeacon:退出区域时从不调用 didDetermineState
问题描述
我正在尝试监视区域并在应用程序被杀死时检测信标(在前台一切正常)。我读过它应该足以设置allowsBackgroundLocationUpdates=true
和pausesLocationUpdatesAutomatically=false
唤醒应用程序,但前提是检测到进入/退出事件。
现在,我的问题是,当我关闭信标时,didDetermineState
永远didExitRegion
不会被调用。如果我明确请求状态,它会返回它仍在该区域内。我错过了什么?
这是我的代码,完全在 AppDelegate 中。
func requestLocationPermissions() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
startMonitoring()
}
func startMonitoring() {
let constraint = CLBeaconIdentityConstraint(uuid: Config.Beacons.uuid, major: Config.Beacons.major, minor: Config.Beacons.minor)
let beaconRegion = CLBeaconRegion(beaconIdentityConstraint: constraint, identifier: Config.Beacons.beaconID)
beaconRegion.notifyEntryStateOnDisplay = true
locationManager.startMonitoring(for: beaconRegion)
locationManager.startRangingBeacons(satisfying: constraint)
}
func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
if state == .inside {
print("AppDelegate: inside beacon region")
} else {
print("AppDelegate: outside beacon region")
}
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("AppDelegate: entered region")
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
print("AppDelegate: exited region")
}
func locationManager(_ manager: CLLocationManager, didRange beacons: [CLBeacon], satisfying beaconConstraint: CLBeaconIdentityConstraint) {
guard beacons.count > 0 else {
let constraint = CLBeaconIdentityConstraint(uuid: Config.Beacons.uuid, major: Config.Beacons.major, minor: Config.Beacons.minor)
locationManager.requestState(for: CLBeaconRegion(beaconIdentityConstraint: constraint, identifier: Config.Beacons.beaconID))
return
}
// Other stuff
}
解决方案
一些提示:
- 您必须向用户请求并获得“始终”位置权限才能获得区域退出回调。
- 如果上述被批准,区域退出回调应该在信标停止传输后 30 秒发生。
- 设置 locationManager.notifyEntryStateOnDisplay=true ,当显示屏亮起时,这将为您提供额外 10 秒的背景扫描。
- 如果您遇到上述问题,请打开信标测距并记录您的信标区域每秒检测到的信标数量。这将告诉你 iOS 是否真的相信信标仍在被看到。如果您启用了 (3) 中的设置,则每次点亮显示屏时,您应该获得 10 秒的测距回调。
- 如果您完全停止获取测距回调,这可能表明存在权限问题。
确保您确实始终授予位置权限,而不仅仅是“使用时”权限。
推荐阅读
- javascript - 使用 javascript 获取 Geojson 属性列表
- typescript - 如何创建将字段添加到任何给定对象类型的自定义实用程序类型?
- python - Selenium 无法设置文本
- canopen - CANOpen 多客户端 单服务器 SDO 通信
- c# - 如何检索在所有表单中选择的复选框列表?
- r - 如何删除df中非常特殊的字符
- ios - 我怎么能想到 Scene Delegate willconnnectTo 方法?
- amazon-web-services - 如何在 Amazon ECS 中运行外部运行状况检查命令?
- google-chrome-extension - 清单 v3 中的 Google Analytics(分析)
- java - 比较 2 ArrayList java