在iOS 14上检查用户位置权限状态 [英] Checking user location permission status on iOS 14

查看:449
本文介绍了在iOS 14上检查用户位置权限状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想检查我是否可以访问iOS14上的用户位置&我找到了这段代码,但XCode(12)对此大吼:

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

这是代码:

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()':

In iOS 14 'authorizationStatus()' is deprecated :

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

您应该改用locationManagerDidChangeAuthorization:

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

这篇关于在iOS 14上检查用户位置权限状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆