Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking user location permission status on iOS 14

So I wanted to check if I have access to the user location on iOS14 or not & I found this code but XCode(12) yells at me with this:

'authorizationStatus()' was deprecated in iOS 14.0

And here is the code:

func hasLocationPermission() -> Bool {
       var hasPermission = false
       if CLLocationManager.locationServicesEnabled() {
           switch CLLocationManager.authorizationStatus() { // <= 'authorizationStatus()' was deprecated in iOS 14.0
           case .notDetermined, .restricted, .denied:
               hasPermission = false
           case .authorizedAlways, .authorizedWhenInUse:
               hasPermission = true
           @unknown default:
               hasPermission = false
             }
       } else {
            hasPermission = false
       }
        return hasPermission
}

So what should I use instead?

like image 786
Ahmadreza Avatar asked Mar 03 '23 02:03

Ahmadreza


1 Answers

iOS 14 and earlier version

func locationAuthorizationStatus() -> CLAuthorizationStatus {
    let locationManager = CLLocationManager()
    var locationAuthorizationStatus : CLAuthorizationStatus
    if #available(iOS 14.0, *) {
        locationAuthorizationStatus =  locationManager.authorizationStatus
    } else {
        // Fallback on earlier versions
        locationAuthorizationStatus = CLLocationManager.authorizationStatus()
    }
    return locationAuthorizationStatus
}
like image 170
Manish Nahar Avatar answered Mar 11 '23 17:03

Manish Nahar