iOS请求始终授权仅第一次显示权限警报

iOS请求始终授权仅第一次显示权限警报,ios,swift,location,mapkit,mapbox,Ios,Swift,Location,Mapkit,Mapbox,第一次,当我通过xcode在设备上安装iOS应用程序且位置服务关闭时,requestAlwaysAuthorization()显示“打开位置服务”,将用户推到设备设置以打开位置服务,然后用户返回应用程序并将看到“权限”警报(有三个选项始终、使用和从不)。如果用户点击“始终”选项,然后完全关闭应用程序并关闭位置服务,然后再次打开应用程序,则不会显示“打开位置服务”警报。这是我的代码: override func viewDidLoad() { super.viewDidLoad()

第一次,当我通过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>
n位置始终和何时使用说明
我的应用程序需要你的位置才能工作
N位置始终使用说明
我的应用程序需要你的位置才能工作
NSLocationWhenUse用途说明
我的应用程序需要你的位置才能工作

我正在iOS 11和iOS 12上测试,我不知道出了什么问题。

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

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

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

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

您只请求一次身份验证。。。如果用户授予权限,则您无需再次询问,如果用户拒绝,则您无法再次提示。如果拒绝,您需要以不同的方式处理。您将用户推到设置菜单中的应用程序设置,用户必须从中启用there@Scriptable感谢您的回复,我试图通过“Prefs:root=privacy&path=location”显示一个警报,将用户推送到设置/隐私/位置服务,但它只显示设置页面。您知道如何将用户推送到隐私->位置服务吗?
如果让url=url(字符串:UIApplicationOpenSettingsURLString){UIApplication.shared.open(url:url)}
UIApplicationOpenSettingsURLString打开我的应用程序设置,如果位置服务关闭,我无法更改应用程序设置中的权限,我需要打开定位服务。你已经错过了我第一次评论的要点。如果用户拒绝您的应用程序的位置权限,则您需要在应用程序设置中进行更改。问题不在于全局位置服务权限,而在于您的应用程序的权限。请参阅下面代码中的拒绝案例您只请求验证一次。。。如果用户授予权限,则您无需再次询问,如果用户拒绝,则您无法再次提示。如果拒绝,您需要以不同的方式处理。您将用户推到设置菜单中的应用程序设置,用户必须从中启用there@Scriptable感谢您的回复,我试图通过“Prefs:root=privacy&path=location”显示一个警报,将用户推送到设置/隐私/位置服务,但它只显示设置页面。您知道如何将用户推送到隐私->位置服务吗?
如果让url=url(字符串:UIApplicationOpenSettingsURLString){UIApplication.shared.open(url:url)}
UIApplicationOpenSettingsURLString打开我的应用程序设置,如果位置服务关闭,我无法更改应用程序设置中的权限,我需要打开定位服务。你已经错过了我第一次评论的要点。如果用户拒绝您的应用程序的位置权限,则您需要在应用程序设置中进行更改。问题不在于全局位置服务权限,而在于您的应用程序的权限。查看下面代码中的拒绝案例我的问题是如何在位置服务关闭时显示“打开位置服务以允许…确定您的位置”警报?如果我应该实现自定义警报,我如何将用户路由到设置->隐私->位置服务?我不知道您如何直接进入全局位置服务屏幕。看:我认为这是不可能的。避免在应用中使用“prefs:root”或“App prefs:root”,否则应用将被应用商店拒绝。只需打开设置页面。我的问题是当位置服务关闭时,如何显示“打开位置服务以允许…确定您的位置”警报?如果我应该实现自定义警报,我如何将用户路由到设置->隐私->位置服务?我不知道您如何直接进入全局位置服务屏幕。看:我认为这是不可能的。避免在应用中使用“prefs:root”或“App prefs:root”,否则应用将被应用商店拒绝。只需打开设置页面。