开发者

Objective C - CLLocationManager find out when "Allow" or "Don't allow" is clicked

When doing CLLocationManager, is there a delegate method that gets called when a user clicked the "Allow" or "Don't allow" prompt that request to use Location?

I tried this but this doesn't get called after a user "Allow" or "Don't allow".

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;

开发者_Python百科Also, is there a variable that will tell me what the user selected?

I tried the below, but that always returns true.

locationManager.locationServicesEnabled

Thank you,

Tee


There is a delegate method for that

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
        // user allowed
    }

}


[CLLocationManager locationServicesEnabled] only tells your if the location service are enabled on the device.

[CLLocationManager authorizationStatus] returns the actual status you're looking for.


You'll have to implement didFailWithError: method:

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {

    if ([error domain] == kCLErrorDomain) {

        // We handle CoreLocation-related errors here
        switch ([error code]) {
        // "Don't Allow" on two successive app launches is the same as saying "never allow". The user
        // can reset this for all apps by going to Settings > General > Reset > Reset Location Warnings.
        case kCLErrorDenied:

        case kCLErrorLocationUnknown:

        default:
            break;
        }

    } else {
    // We handle all non-CoreLocation errors here
    }
}

EDIT: Looking at CLLocationManager's reference I've found this:

+ (CLAuthorizationStatus)authorizationStatus

Return Value A value indicating whether the application is authorized to use location services.

Discussion The authorization status of a given application is managed by the system and determined by several factors. Applications must be explicitly authorized to use location services by the user and location services must themselves currently be enabled for the system. This authorization takes place automatically when your application first attempts to use location services.


locationManager.locationServicesEnabled indicates whether location services is available, but not necessarily mean they are allowed for your app.

Use CLLocationManager.authorizationStatus() if you need to find out the status at a point in time, or implement

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;

Take note since iOS 8, the authorization request does not takes place automatically when your application first attempts to use location services. You need to call requestWhenInUseAuthorization() explicitly before you call startUpdatingLocation() on your CLLocationManager instance.

And make sure you have the NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist, depending on the type of authorization you are after. If these are missing, there are no errors, no logs, no hints, no nothing that will point you in the right direction :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜