ios - CLLocationManager 的 AuthorizationStatus 在 iOS 14 上已弃用
问题描述
我使用此代码检查我是否有权访问用户位置
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
,的一个属性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:
...
}
推荐阅读
- android - 如何让 Pinch Zoom 在 webview_flutter 插件上工作
- google-cloud-firestore - Firestore 安全规则:在 get() 调用中传递请求参数?
- functional-programming - 列表 l 中的 mappedList 和 x 是什么?
- r - 使用 gsub 忽略 R 中的正则表达式
- excel - 如何在两个 Excel 工作簿之间复制数据?
- vb.net - 如何在没有循环的情况下直接查找多个文本
- javascript - 根据下面给出的逻辑实现父子关系逻辑
- excel - 复制转置粘贴到下一行的基本代码
- mongodb - 将 AWS EC2 Cloud9 连接到 mongoDB Atlas 集群的最佳方法是什么?
- python - ImportError:没有命名的模块
文件夹内的模块