问题描述
我正在尝试监视区域并在应用程序被终止时检测信标(在前台一切正常)。我已经读过设置 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,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 秒的测距回调。
- 如果您完全停止接收测距回调,这可能表明存在权限问题。
确保您确实始终授予位置权限,而不仅仅是“使用时”权限。