这是我的简单代码,我正在尝试监视进入/退出事件。我面临的问题: 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)")
}
}