【问题标题】:iOS Requesting Always Authorization Shows Permission Alert Just For the First TimeiOS 请求始终授权仅首次显示权限警报
【发布时间】: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


【解决方案1】:

您只需请求一次身份验证...如果用户授予权限,则您无需再次询问,如果他们拒绝,您将无法再次提示。如果被拒绝,您需要以不同的方式处理它。您将用户推送到设置菜单中的应用程序设置,用户必须从那里启用

switch CLLocationManager.authorizationStatus() {
    case .notDetermined, .restricted:
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
    case .authorizedWhenInUse, .authorizedAlways:
        ...
    case .denied:
        // present an alert advising the user that they need to go to the settings menu to enable the permissions as they have previously denied it. 
        // if the user presses Open Settings on the alert....
        if let url = URL(string: UIApplicationOpenSettingsURLString) { 
            UIApplication.shared.open(url: url) 
        }
    }

【讨论】:

  • 我的问题是如何在位置服务关闭时显示“打开位置服务以允许...确定您的位置”警报?如果我应该实现我的自定义警报,我如何将用户路由到设置 -> 隐私 -> 位置服务?
  • 我不知道你是如何直接进入全球定位服务屏幕的。见:stackoverflow.com/questions/37654132/…
  • 我认为这是不可能的。避免在您的应用程序中使用“prefs:root”或“App-Prefs:root”,否则应用程序将被 App Store 拒绝。只需打开设置页面。
猜你喜欢
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
  • 2014-11-18
  • 2014-04-15
  • 1970-01-01
  • 1970-01-01
  • 2017-08-24
相关资源
最近更新 更多