Here is my simple code, where I'm trying to monitor enter/exit events. The problem I faced: DidEnterRegion and DidExitRegion are being called several times: the first one - when it actually happens and another calls - when I come to background and then return back to foreground in range between ~15-30 seconds after the first call of DidEnterRegion/DidExitRegion
What is the reason of this triggers? Why is this happening?
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)")
}
}