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

didEnterRegion, didExitRegion not being called

Hi, I've been experimenting with region monitoring in order to show an alert or a local notification when the user is within the set region. As a first step, I added a print line to see if it works on the debug area. However, while the other lines are being printed, I'm not getting anything for didEnterRegion and didExitRegion.

I am simulating the location to be in/outside of the given region but I am having no luck. It will be great if someone could look at the code below and see what I've missed. Thank you.

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

var manager = CLLocationManager?()



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        manager = CLLocationManager()
        let latitude: CLLocationDegrees = 48.858400
        let longitude: CLLocationDegrees = 2.294500
        let center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
        let radius: CLLocationDistance = CLLocationDistance(100.0)
        let identifier: String = "Notre Dame"

        let currRegion = CLCircularRegion(center: center, radius: radius, identifier: identifier)

        manager?.distanceFilter = 10
        manager?.desiredAccuracy = kCLLocationAccuracyBest
        currRegion.notifyOnEntry = true
        currRegion.notifyOnExit = true

        manager?.requestWhenInUseAuthorization()


        manager?.delegate = self
        manager?.pausesLocationUpdatesAutomatically = true

        manager?.startMonitoringForRegion(currRegion)
        manager?.startUpdatingLocation()    


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func locationManager(manager: CLLocationManager, didStartMonitoringForRegion 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)  {
        NSLog("Entered")
    }

    func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
        NSLog("Exited")
    }


}

1 Answer

Problem solved!

This had to be called:

manager?.requestAlwaysAuthorization()

instead of :

manager?.requestWhenInUseAuthorization()