Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Lucas O
Lucas O
3,714 Points

didEnterRegion and didExitRegion not being called (tried other online solutions)

I am developing an app through xCode which uses geofencing and region monitoring. Through monitoring these regions upon enter and exit, a pop up is meant to be called to display to the user the location they are now in (or exiting) said location. When I use my .gpx file to simulate leaving and entering the location or entering/leaving the location myself, unfortunately the enter and exit functions never get called.

Before recent updates in Xcode my code worked perfectly and enter and exit functions were being called. They do not call now and have not been for quite some time. I've done a lot of research on the matter and have found with updates in Xcode, changing manager.requestWhenInUseAuthorization() to manager.requestAlwaysAuthorization() and updating plist files accordingly should resolve this error (manager = CLLocationManager). I've tried this and have updated my code but to no avail.

I have the P-list files of Privacy - Location Always and When In Use Usage Description, Privacy - Location When In Use Usage Description and Privacy - Location Usage Description in my app.

I've placed my code pertaining to this problem below and I and others would greatly appreciate any help or ideas on how to fix this problem as it does seem to be re-occuring in the updated xCode. Thank you:)

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager = CLLocationManager()
    mapView.delegate = self
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    locationManager.startUpdatingLocation()
    mapView.showsUserLocation = true
    mapView.setUserTrackingMode(.follow, animated: true)

    let title = "Oakville Test"
    let coordinate = CLLocationCoordinate2DMake(43.477521, -79.712430)
    let regionRadius = 150.0

    // setup region
    let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude), radius: regionRadius, identifier: title)
    region.notifyOnEntry = true
    region.notifyOnExit = true
    locationManager.startMonitoring(for: region)
    locationManager.startUpdatingLocation()

    // setup annotation
    let annotationView = MKPointAnnotation()
    annotationView.coordinate = coordinate;
    annotationView.title = "\(title)";
    mapView.addAnnotation(annotationView)

    // setup circle
    let circle = MKCircle(center: coordinate, radius: regionRadius)
    mapView.add(circle)

}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer! {
    if overlay is MKPolyline {
        let renderer = MKPolylineRenderer(overlay: overlay)
        renderer.strokeColor = UIColor.blue.withAlphaComponent(0.6)
        renderer.lineWidth = 5.0
        renderer.fillColor = UIColor.blue.withAlphaComponent(0.7)
        return renderer
    } else {
        let circleRenderer = MKCircleRenderer(overlay: overlay)
        circleRenderer.strokeColor = UIColor.red.withAlphaComponent(0.4)
        circleRenderer.lineWidth = 1.0
        circleRenderer.fillColor = UIColor.red.withAlphaComponent(0.4)
        return circleRenderer
    }
    return nil
}

func showAlert(_ title: String) {
    let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
    let noAction = UIAlertAction(title: "No", style: .default, handler: nil)
    let yesAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: nil)
    alert.addAction(yesAction)
    alert.addAction(noAction)
    self.present(alert, animated: true, completion: nil)
}

func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
    print("The monitored regions are: \(manager.monitoredRegions)")
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let locValue:CLLocationCoordinate2D = manager.location!.coordinate
    print("locations = \(locValue.latitude) \(locValue.longitude)")
}

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    showAlert("You are at \(region.identifier)!")
}

func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
    showAlert("leaving \(region.identifier).")
    }

}