CLLocationManager 的 AuthorizationStatus 在 iOS 14 上已弃用

Alf*_*lfi 8 core-location ios swift ios14

我使用此代码来检查我是否有权访问用户位置

if CLLocationManager.locationServicesEnabled() {
    switch CLLocationManager.authorizationStatus() {
        case .restricted, .denied:
            hasPermission = false
        default:
            hasPermission = true
        }
    } else {
        print("Location services are not enabled")
    }
}
Run Code Online (Sandbox Code Playgroud)

Xcode(12) 用这个警告对我大喊大叫:

'authorizationStatus()' was deprecated in iOS 14.0
Run Code Online (Sandbox Code Playgroud)

那么什么是替代品呢?

Rob*_*Rob 25

现在的属性CLLocationManagerauthorizationStatus。因此,创建一个CLLocationManager实例:

let manager = CLLocationManager()
Run Code Online (Sandbox Code Playgroud)

然后您可以从那里访问该属性:

switch manager.authorizationStatus {
case .restricted, .denied:
    hasPermission = false
default:
    hasPermission = true
}
Run Code Online (Sandbox Code Playgroud)

iOS 14 中有一些与位置相关的更改。请参阅 WWDC 2020位置更新