Coder1000 Coder1000 - 3 months ago 42
Swift Question

How to notify user when he enters a CLLocation?

SITUATION:

I followed the following tutorial:

https://www.raywenderlich.com/95014/geofencing-ios-swift




PROBLEM:

The following functions never get triggered:

AppDelegate.swift

func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
if region is CLCircularRegion {
handleRegionEvent(region)
}
}

func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
if region is CLCircularRegion {
handleRegionEvent(region)
}
}

func handleRegionEvent(region: CLRegion!) {

print("Geofence triggered!")

// Show an alert if application is active
if UIApplication.sharedApplication().applicationState == .Active {
if let message = notefromRegionIdentifier(region.identifier) {
if let viewController = window?.rootViewController {
showSimpleAlertWithTitle("Congratulations", message: "You just found: " + message , viewController: viewController)
}
}
} else {
// Otherwise present a local notification
let notification = UILocalNotification()
notification.alertBody = "You just found: " + notefromRegionIdentifier(region.identifier)!

notification.soundName = "Default";
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
}
}





QUESTION:

The tutorial was written for iOS 8. I am currently on iOS 9.3. What caused this issue in your opinion and how do I fix it ?

Answer

Make sure of two things :-

1.) You have added These to your viewDidLoad() :-

    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = kCLDistanceFilterNone
    locationManager.requestWhenInUseAuthorization()
    locationManager.startMonitoringSignificantLocationChanges()
    locationManager.startUpdatingLocation()

Another alternative to requestWhenInUseAuthorization() and startUpdatingLocation() initialisation in specific to Swift 2.2, since in Swift 2.2 the string literals for selectors is deprecated, and instead there this new operator #selector that you need to be using. :-

you can also use :-

    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = kCLDistanceFilterNone

    locationManager.startMonitoringSignificantLocationChanges()
     if locationManager.respondsToSelector(#selector(locationManager.requestWhenInUseAuthorization)) {
        locationManager.requestWhenInUseAuthorization()
    }
    else {
        locationManager.startUpdatingLocation()
    }
      //Prefer the FIRST ONE.

2.) You have updated your info.plist with :-

NSLocationAlwaysUsageDescription : String :-> I need location.

NSLocationWhenInUseUsageDescription: String :-> I need location.

privacy - location usage description: String :-> I need location.

Edit I need location according to the app's need

PS :- If it still not calls your locationManager functions

Simulator :- look for location settings of your app in your simulator settings.

Device: - Go in settings > Privacy > Location services > Your app > Always.

you also might find this explanation useful : - http://stackoverflow.com/a/26090094/6297658