Checking User Location Permission Status on iOS 14

Checking user location permission status on iOS 14

In iOS 14 'authorizationStatus()' is deprecated :

https://developer.apple.com/documentation/corelocation/cllocationmanager/1423523-authorizationstatus

You should use locationManagerDidChangeAuthorization instead:

func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {

switch manager.authorizationStatus {
case .authorizedAlways , .authorizedWhenInUse:
break
case .notDetermined , .denied , .restricted:
break
default:
break
}

switch manager.accuracyAuthorization {
case .fullAccuracy:
break
case .reducedAccuracy:
break
default:
break
}
}

How to check location authorization status at the click of a button in iOS 14?

You're code works - but have you remembered to add the privacy usage descriptions to the info.plist file?

Add these two entries in the file with your own explanation in the value field, then it should popup in the simulator:

  • Privacy - Location Always and When In Use Usage Description
  • Privacy - Location When In Use Usage Description

How does one check Core Location authorization status in iOS versions below 14?

Before iOS 14, there's the class method authorizationStatus on CLLocationManager, it became an instance property after that. CLLocationManager.h has the details

iOS 13 How to check user is given only Always allow location permission

Actually, I removed CLLocationManager.requestAlwaysAuthorization() from the code.

If you request for Always Authorization, CLLocationManager.authorizationStatus becomes authorizedAlways.

I made it like if app wants always access, user has to go to settings and give the always permission manually

AuthorizationStatus for CLLocationManager is deprecated on iOS 14

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:
...
}

Check if location services are enabled

Add the CLLocationManagerDelegate to your class inheritance and then you can make this check:

Import CoreLocation Framework

import CoreLocation

Swift 1.x - 2.x version:

if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .NotDetermined, .Restricted, .Denied:
print("No access")
case .AuthorizedAlways, .AuthorizedWhenInUse:
print("Access")
}
} else {
print("Location services are not enabled")
}

Swift 4.x version:

if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .notDetermined, .restricted, .denied:
print("No access")
case .authorizedAlways, .authorizedWhenInUse:
print("Access")
}
} else {
print("Location services are not enabled")
}

Swift 5.1 version

if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .notDetermined, .restricted, .denied:
print("No access")
case .authorizedAlways, .authorizedWhenInUse:
print("Access")
@unknown default:
break
}
} else {
print("Location services are not enabled")
}

iOS 14.x

In iOS 14 you will get the following error message:
authorizationStatus() was deprecated in iOS 14.0
To solve this, use the following:
private let locationManager = CLLocationManager()

if CLLocationManager.locationServicesEnabled() {
switch locationManager.authorizationStatus {
case .notDetermined, .restricted, .denied:
print("No access")
case .authorizedAlways, .authorizedWhenInUse:
print("Access")
@unknown default:
break
}
} else {
print("Location services are not enabled")
}


Related Topics



Leave a reply



Submit