AuthorizationStatus for CLLocationManager is deprecated on iOS 14
Asked Answered
C

4

44

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?

Canea answered 26/9, 2020 at 4:13 Comment(0)
F
106

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:
    ...
}
Farinaceous answered 26/9, 2020 at 18:9 Comment(3)
Thanks but I'm getting this Static member 'manager' cannot be used on instance of type 'CLLocationManager'Wolfson
Bug in Xcode beta most likely if I set target to iOS 13 then both examples works using CLLocation literate as original poster and using let manager = CLLocationManager() as you demonstrated, there is no static member error then. WeirdWolfson
If you need to simultaneously support iOS 14 and earlier versions, use #available check.Farinaceous
B
1

Just move the brackets before the dot:

CLLocationManager().authorizationStatus
Binny answered 8/10, 2023 at 14:12 Comment(1)
Unexpected interface name 'CLLocationManager': expected expressionTenno
E
0

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...
Excavate answered 9/3, 2022 at 0:48 Comment(0)
T
0
Let locationManager = CLLocationManager()

change CLLocationManager.authorizationStatus() to manager.authorizationStatus

Telescopy answered 7/5 at 5:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.