首页 > 解决方案 > iBeacon:退出区域时从不调用 didDetermineState

问题描述

我正在尝试监视区域并在应用程序被杀死时检测信标(在前台一切正常)。我读过它应该足以设置allowsBackgroundLocationUpdates=truepausesLocationUpdatesAutomatically=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
 }

标签: iosswiftibeaconbeacon

解决方案


一些提示:

  1. 您必须向用户请求并获得“始终”位置权限才能获得区域退出回调。
  2. 如果上述被批准,区域退出回调应该在信标停止传输后 30 秒发生。
  3. 设置 locationManager.notifyEntryStateOnDisplay=true ,当显示屏亮起时,这将为您提供额外 10 秒的背景扫描。
  4. 如果您遇到上述问题,请打开信标测距并记录您的信标区域每秒检测到的信标数量。这将告诉你 iOS 是否真的相信信标仍在被看到。如果您启用了 (3) 中的设置,则每次点亮显示屏时,您应该获得 10 秒的测距回调。
  5. 如果您完全停止获取测距回调,这可能表明存在权限问题。

确保您确实始终授予位置权限,而不仅仅是“使用时”权限。


推荐阅读