Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLLocationManager AuthorizationStatus callback?

Tags:

ios

swift

In my app I have a tab called "Discover". The Discover tab will use the users current location to find "stuff" near them. Instead of presenting the user with a generic Authorization Request (which typically gets declined, based on research), I present the user with a modal explaining what we're asking for. If they say yes, THEN the actual Authorization message pops up.

However, the user still has the option to say no the prompt. Is there a way to add a callback to the prompt so that once the user selects an option I can see if they accepted or declined?

I have tried this:

func promptForLocationDataAccess() {
    locationManager.requestWhenInUseAuthorization()
    println("test")
}

As expected, the "println" executes at the same time that the request prompt comes up, so I can't do it that way.

The problem is that if the user opts to not use Location Data, the content served will be different than if they had accepted.

Ideally I'm hoping for a callback of some kind, but I'll take any logical direction at this point!

like image 825
matcartmill Avatar asked Jan 14 '15 23:01

matcartmill


2 Answers

You can use the locationManager:didChangeAuthorizationStatus: CLLocationManagerDelegate method as a "callback" of sorts.

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusDenied) {
        // The user denied authorization
    }
    else if (status == kCLAuthorizationStatusAuthorized) {
        // The user accepted authorization
    }
}

And in Swift (update suggested by user Michael Marvick, but rejected for some reason...):

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if (status == CLAuthorizationStatus.denied) {
        // The user denied authorization
    } else if (status == CLAuthorizationStatus.authorizedAlways) {
        // The user accepted authorization
    } 
}
like image 170
Lyndsey Scott Avatar answered Nov 14 '22 06:11

Lyndsey Scott


When the authorisation status for location changes, the delegate method didChangeAuthorizationStatus: will be called.

When you call requestWhenInUseAuthorization the first time after your app is installed the delegate method will be called with status kCLAuthorizationStatusNotDetermined (0).

If the user declines location services access then the delegate method will be called again with status kCLAuthorizationStatusDenied (2).

If the user approves location services access then the delegate method will be called again with status kCLAuthorizationStatusAuthorizedAlways (3) or kCLAuthorizationStatusAuthorizedWhenInUse (4) depending on the permission that was requested.

On subsequent executions of your app the delegate method will receive status kCLAuthorizationStatusDenied or kCLAuthorizationStatusAuthorizedAlways/kCLAuthorizationStatusAuthorizedWhenInUse after calling requestWhenInUseAuthorization based on the current state of location services permission for the app in the device settings.

like image 6
Paulw11 Avatar answered Nov 14 '22 07:11

Paulw11