AuthorizationStatus for CLLocationManager is deprecated on iOS 14

23.2k views Asked by At

I use this code to check if I have access to the user location or not

if CLLocationManager.locationServicesEnabled() {
    switch CLLocationManager.authorizationStatus() {
        case .restricted, .denied:
            hasPermission = false
        default:
            hasPermission = true
        }
    } else {
        print("Location services are not enabled")
    }
}

And Xcode(12) yells at me with this warning:

'authorizationStatus()' was deprecated in iOS 14.0

So what is the replacement?

3

There are 3 answers

3
Rob On BEST ANSWER

It is now a property of CLLocationManager, authorizationStatus. So, create a CLLocationManager instance:

let manager = CLLocationManager()

Then you can access the property from there:

switch manager.authorizationStatus {
case .restricted, .denied:
    ...
default:
    ...
}

There are a few location related changes in iOS 14. See WWDC 2020 What's new in location.


Needless to say, if you also need to support iOS versions prior to 14, then just add the #available check, e.g.:

let authorizationStatus: CLAuthorizationStatus

if #available(iOS 14, *) {
    authorizationStatus = manager.authorizationStatus
} else {
    authorizationStatus = CLLocationManager.authorizationStatus()
}

switch authorizationStatus {
case .restricted, .denied:
    ...
default:
    ...
}
0
KeithTheBiped On

Objective C Version:

In Class Interface

@property (nonatomic, strong) CLLocationManager *locationManager;

In Class Code

- (id) init {
self = [super init];
if (self != nil) {
    self.locationManager = [[CLLocationManager alloc]init];
}
return self;
}

-(CLLocation*)getLocation
{
    CLAuthorizationStatus status = [self.locationManager authorizationStatus];
    if (status == kCLAuthorizationStatusNotDetermined)
    {
        [self promptToEnableLocationServices];
        return nil;
    }
 etc...
1
Display Name On

Just move the brackets before the dot:

CLLocationManager().authorizationStatus