ios - CLLocationManager 的 AuthorizationStatus 在 iOS 14 上已弃用

标签 ios swift core-location ios14

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

if CLLocationManager.locationServicesEnabled() {
    switch CLLocationManager.authorizationStatus() {
        case .restricted, .denied:
            hasPermission = false
        default:
            hasPermission = true
        }
    } else {
        print("Location services are not enabled")
    }
}
Xcode(12) 用这个警告对我大喊大叫:
'authorizationStatus()' was deprecated in iOS 14.0
那么替代品是什么呢?

最佳答案

它现在是 CLLocationManager 的属性(property), authorizationStatus .所以,创建一个 CLLocationManager实例:

let manager = CLLocationManager()
然后您可以从那里访问该属性:
switch manager.authorizationStatus {
case .restricted, .denied:
    ...
default:
    ...
}
iOS 14 中有一些与位置相关的更改。请参阅 WWDC 2020 What's new in location .

不用说,如果您还需要支持 14 之前的 iOS 版本,那么只需添加 #available检查,例如:
let authorizationStatus: CLAuthorizationStatus

if #available(iOS 14, *) {
    authorizationStatus = manager.authorizationStatus
} else {
    authorizationStatus = CLLocationManager.authorizationStatus()
}

switch authorizationStatus {
case .restricted, .denied:
    ...
default:
    ...
}

关于ios - CLLocationManager 的 AuthorizationStatus 在 iOS 14 上已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64073811/

相关文章:

ios - 如何在下面给出的 Hidekeyboard 函数中包含多个 UITextField?

ios - 未找到目标,请重新连接设备,Xcode :Device Support File

objective-c - [任何对象]?没有名为 'mutableCopy' 的成员

ios - 地理围栏的准确性取决于 locationManager 的准确性或距离过滤器吗?

ios - 滚动 UIViewController 标题

ios - 奇怪但易于理解的错误... UIActivityIndi​​catorView stopAnimating 不起作用

iphone - 在 iphone 中重新加载 DatePicker

swift - 两个 A 和 B 位置之间的路线

ios - 如何像 'Moment' 应用程序那样跟踪 iPhone 使用情况?

objective-c - 如何在 Objective-C 中使用 SQLite3 中的保存点