Coder1000
Coder1000

Reputation: 4461

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 ?

Upvotes: 1

Views: 211

Answers (3)

Dravidian
Dravidian

Reputation: 9945

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 : - https://stackoverflow.com/a/26090094/6297658

Upvotes: 1

Sabby
Sabby

Reputation: 403

initialize your location manager in app delegate on did finish launching

Upvotes: 0

Gene De Lisa
Gene De Lisa

Reputation: 3838

You didn't show the code that you use to set up CL - which is probably where your problem lies.

Did you edit info.plist?

Are you requesting permission?

Did you call one of the start functions on the CL manager?

Upvotes: 2

Related Questions