I've set the delegate, created a region and i've read all the documentation. Every time the user's location changes and is outside of the specified region, locationManager(manager: CLLocationManager, didExitRegion region: CLRegion)
isn't being called. I've seen it get called once and then it stopped doing it. I've spent a lot of time trying to figure out what's going on.
Quick overview of what i'm doing:
When the app launches I call locationManager.startUpdatingLocation()
which in turn calls the delegates locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
, I do a quick request to the server with some information I need, create a CLCircularRegion
and proceed to call locationManager.stopUpdatingLocation()
. In that method I call my startMonitoringForRegion(_:)
.
Is it maybe because every time I create a new region it uses the same identifier? Apple's docs say it's okay to use the same identifier, because it will replace the previous one, but I'm wondering if that's causing the delegate not to call didExitRegion
.
My code is below. Thank you.
func configureLocation() {
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.allowsBackgroundLocationUpdates = false
locationManager.pausesLocationUpdatesAutomatically = true
// Distance filter to update location
//locationManager.distanceFilter = 10.0
// Begin updating user location
locationManager.startUpdatingLocation()
} else {
let title = "Location disabled"
let message = "To enable location services go to Settings > Privacy > Location Services"
self.locationServicesAlertMessage(title, message: message)
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("didUpdateCalled")
/*
if UIApplication.sharedApplication().applicationState == .Active {
print("ACTIVE UPDATE: location")
} else if UIApplication.sharedApplication().applicationState == .Background {
print("BACKGROUND UPDATE: location")
}
*/
if firstLocationUpdate {
let center = CLLocationCoordinate2D(latitude: (manager.location?.coordinate.latitude)!, longitude: (manager.location?.coordinate.longitude)!)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpanMake(0.01, 0.01))
self.mapView.setRegion(region, animated: true)
self.firstLocationUpdate = false
}
// If the user is travelling less than 5 mph, update location
if manager.location?.speed <= 3.5 {
self.updateLongAndLat(manager.location!) { (lat, long) in
print("LAT: \(lat)")
print("LONG: \(long)")
if lat != nil && long != nil {
let center = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
let monitoredRegion = CLCircularRegion(center: center, radius: 10.0, identifier: "UserRegion")
monitoredRegion.notifyOnEntry = false
self.locationManager.startMonitoringForRegion(monitoredRegion)
self.locationManager.stopUpdatingLocation()
}
}
}
}
func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
print("EXITED REGION")
print("Identifier: \(region.identifier)")
if manager.location?.speed <= 3.5 {
self.updateLongAndLat(manager.location!, completion: { (lat, long) in
if lat != nil && long != nil {
let center = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
let monitoredRegion = CLCircularRegion(center: center, radius: 10.0, identifier: "UserRegion")
self.locationManager.startMonitoringForRegion(monitoredRegion)
}
})
}
}