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
Is There an Kotlin Equivalent 'With' Function in Swift
Ambigious Reference to Member Request() Issues with Alamofire After Migration to Swift 3
Is .Playground a Swift File? Who Can 'See' It
Applewatch Messages Url Works Hard Coded But Not with Variables
How to Convert an Anykeypath to a Writablekeypath
Protocol Can Only Be Used as a Generic Constraint
How to Implement a 'Next' Property to a Caseiterable Enum in Swift
Ar with iOS: Putting a Light in the Scene Makes Everything Black
Using Iboutlet from Another Class in Swift
Play Sound with a Little Delay
Error "No Such Module" When Installed Framework with Pod in Swift 3
Adding Animation to Tabviews in Swiftui When Switching Between Tabs
Type of Expression Is Ambiguous Without More Context in Xcode 11
Check If a Character Is Lowercase or Uppercase