在 iOS 14 检查用户位置权限状态
Checking user location permission status on iOS 14
所以我想检查我是否可以访问 iOS14 上的用户位置并且我找到了这段代码但是 XCode(12) 对我大喊大叫:
'authorizationStatus()' was deprecated in iOS 14.0
这是代码:
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
}
那我应该用什么呢?
在 iOS 14 'authorizationStatus()' 中已弃用:
https://developer.apple.com/documentation/corelocation/cllocationmanager/1423523-authorizationstatus
您应该改用 locationManagerDidChangeAuthorization:
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
}
}
iOS 14 Check user is allow permission
extension CLLocationManager {
func checkLocationPermission() {
if self.authorizationStatus != .authorizedWhenInUse && self.authorizationStatus != .authorizedAlways {
self.requestAlwaysAuthorization()
}
}
}
Use
self.LocationManager.checkLocationPermission()
iOS 14 及更早版本
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
}
所以我想检查我是否可以访问 iOS14 上的用户位置并且我找到了这段代码但是 XCode(12) 对我大喊大叫:
'authorizationStatus()' was deprecated in iOS 14.0
这是代码:
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
}
那我应该用什么呢?
在 iOS 14 'authorizationStatus()' 中已弃用:
https://developer.apple.com/documentation/corelocation/cllocationmanager/1423523-authorizationstatus
您应该改用 locationManagerDidChangeAuthorization:
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
}
}
iOS 14 Check user is allow permission
extension CLLocationManager {
func checkLocationPermission() {
if self.authorizationStatus != .authorizedWhenInUse && self.authorizationStatus != .authorizedAlways {
self.requestAlwaysAuthorization()
}
}
}
Use
self.LocationManager.checkLocationPermission()
iOS 14 及更早版本
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
}