【发布时间】:2019-04-17 08:38:12
【问题描述】:
当我通过 xcode 在设备上安装我的 iOS 应用程序并且位置服务关闭时,requestAlwaysAuthorization() 显示“打开位置服务”,将用户推送到设备设置以打开位置服务,然后用户返回应用程序并会看到“权限”警报(始终有三个选项,使用时和从不)。如果用户点击始终选项然后完全关闭应用程序并关闭位置服务然后再次打开应用程序,则不会显示“打开位置服务”警报。这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
NotificationCenter.default.addObserver(self, selector: #selector(checkLocationService), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)
}
@objc func checkLocationService() {
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .denied, .notDetermined, .restricted:
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
case .authorizedWhenInUse, .authorizedAlways:
...
}
} else {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
}
}
我在 Info.plist 中添加了所有三个位置键:
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>My app need your location to work</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>My app need your location to work</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>My app need your location to work</string>
我正在 iOS 11 和 12 上进行测试,但我不知道出了什么问题。
【问题讨论】:
-
您只请求一次身份验证...如果用户授予权限,则您无需再次询问,如果他们拒绝,您将无法再次提示。如果被拒绝,您需要以不同的方式处理它。您将用户推送到设置菜单中的应用程序设置,用户必须从那里启用
-
@Scriptable 感谢您的回复,我试图通过“Prefs:root=Privacy&path=LOCATION”向用户推送设置/隐私/位置服务的警报,但它只显示设置页面。您知道如何将用户推送到隐私 -> 位置服务吗?
-
if let url = URL(string: UIApplicationOpenSettingsURLString) { UIApplication.shared.open(url: url) } -
UIApplicationOpenSettingsURLString 打开我的应用设置,如果位置服务关闭,我无法更改应用设置中的权限,我需要打开位置服务。
-
你错过了我第一条评论的重点。如果用户拒绝您的应用程序的位置权限,那么您需要在您的应用程序设置中更改它。问题不是全球位置服务权限,而是您的应用程序的权限。请参阅下面代码中的 denied 案例
标签: ios swift location mapkit mapbox