Я использую следующий код для получения CLCircularRegion. Но didEnterRegion и didExitRegion не вызываются.
Примечание: didUpdateLocations вызывает
Мой код:
import CoreLocation
class HomeViewController: BaseViewController, CLLocationManagerDelegate {
var locManager : CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
Utility.setLoginStatus()
if (CLLocationManager.locationServicesEnabled()) {
locManager = CLLocationManager()
locManager.delegate = self
locManager.desiredAccuracy = kCLLocationAccuracyBest
locManager.allowsBackgroundLocationUpdates = true
locManager.activityType = .fitness
locManager.requestAlwaysAuthorization()
locManager.requestWhenInUseAuthorization()
locManager.startUpdatingLocation()
}
let myLocaton = CLLocationCoordinate2DMake(Double( 18.66671), Double(73.911622))
self.monitorRegionAtLocation(center: myLocaton, identifier: "HOME")
}
func monitorRegionAtLocation(center: CLLocationCoordinate2D, identifier: String ) {
// Make sure the devices supports region monitoring.
if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
// Register the region.
let region = CLCircularRegion(center: center,
radius: 250, identifier: identifier)
region.notifyOnEntry = true
region.notifyOnExit = true
locManager.startMonitoring(for: region)
}
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("didEnterRegion")
if let region = region as? CLCircularRegion {
let identifier = region.identifier
self.showAlertView(title: AlertTitle, message: "Did Arrive: \(region.identifier) region.")
}
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
print("didExitRegion")
if let region = region as? CLCircularRegion {
let identifier = region.identifier
self.showAlertView(title: AlertTitle, message: "Did Exit: \(region.identifier) region")
}
}
}