监控信标时会多次调用 DidEnterRegion 和 DidExitRegion

问题描述

这是我的简单代码,我试图在其中监视进入/退出事件。 我面临的问题:DidEnterRegion 和 DidExitRegion 被多次调用:第一个 - 当它实际发生时,另一个调用 - 当我来到后台,然后在第一次调用后约 15-30 秒的范围内返回到前台DidEnterRegion/DidExitRegion

这个触发的原因是什么?为什么会发生这种情况?

class AppDelegate: UIResponder,UIApplicationDelegate,CLLocationManagerDelegate {
    var window: UIWindow?

    var locationManager = CLLocationManager()
    var beaconRegion: CLBeaconRegion!

    func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        confugureLocationManager()
        configureRegion()

        return true
    }

    private func confugureLocationManager() {
        locationManager.delegate = self

        if CLLocationManager.authorizationStatus() != .authorizedAlways {
            locationManager.requestAlwaysAuthorization()
        }

        print("monitored regions: \(String(describing: locationManager.monitoredRegions))")
    }

    private func configureRegion() {
        let uuid = UUID(uuidString: "04C7E2F3-42A5-5127-B066-502C8A27EB85")!
        beaconRegion = CLBeaconRegion(uuid: uuid,identifier: uuid.uuidString)
    }

    func locationManager(_ manager: CLLocationManager,didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedAlways {
            if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
                locationManager.startMonitoring(for: beaconRegion)
            }
        }
    }

    func locationManager(_ manager: CLLocationManager,didDetermineState state: CLRegionState,for region: CLRegion) {
        if state == .inside {
            print("Did determine INSIDE state for region.")
        } else {
            if state == .outside {
            print("Did determine OUTSIDE state for region.")
            }

            if state == .unkNown {
                print("Did determine UNKNowN state for region.")
            }
        }
    }

    func locationManager(_ manager: CLLocationManager,didStartMonitoringFor region: CLRegion) {
        print("did start monitoring")
    }

    func locationManager(_ manager: CLLocationManager,didEnterRegion region: CLRegion) {
        print("Did enter region: \(region.identifier),manager: \(manager)")
    }

    func locationManager(_ manager: CLLocationManager,didExitRegion region: CLRegion) {
        print("Did exit region: \(region.identifier)")
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)