gpt4 book ai didi

swift - 在 iOS 14 上检查用户位置权限状态

转载 作者:行者123 更新时间:2023-12-04 00:54:24 28 4
gpt4 key购买 nike

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

'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()”:
https://developer.apple.com/documentation/corelocation/cllocationmanager/1423523-authorizationstatus
您应该使用 locationManagerDidChangeAuthorization 代替:

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

关于swift - 在 iOS 14 上检查用户位置权限状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63788488/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com