Checking user location permission status on iOS 14

耗尽温柔 提交于 2021-01-01 04:53:20

问题


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?


回答1:


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



回答2:


iOS 14 Check user is allow permission

extension CLLocationManager {
  func checkLocationPermission() {
    if self.authorizationStatus != .authorizedWhenInUse && self.authorizationStatus != .authorizedAlways {
      self.requestAlwaysAuthorization()
    }
  }
}

Use

self.LocationManager.checkLocationPermission()


来源:https://stackoverflow.com/questions/63788488/checking-user-location-permission-status-on-ios-14

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!