简体   繁体   English

iOS要求始终授权的iOS首次显示权限警报

[英]iOS Requesting Always Authorization Shows Permission Alert Just For the First Time

Fist time when I install my iOS app on a device via xcode and location service is off, requestAlwaysAuthorization() shows "Turn on location services" that push user to device Settings to turn on location service, then user comes back to the app and would see "permission" alert(with three options always, while using and never). 当我通过xcode在设备上安装iOS应用并关闭位置服务的第一时间,requestAlwaysAuthorization()显示“打开位置服务”,将用户推送到设备设置以打开位置服务,然后用户返回到该应用,请参阅“权限”警报(始终使用三个选项,同时使用并且永不使用)。 If user taps on always option then closes the app completely and turns location service off then opens app again, "Turn on location services" alert is not displayed. 如果用户点击始终选项,然后完全关闭应用程序并关闭位置服务,然后再次打开应用程序,则不会显示“打开位置服务”警报。 This is my code: 这是我的代码:

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

I've added all three location keys in the Info.plist: 我已经在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>

I am testing on iOS 11 and 12 and I have no idea what is wrong. 我正在iOS 11和12上进行测试,不知道出了什么问题。

You only request auth once... if the user grants permission then you dont need to ask again and if they refuse you can't prompt again. 您只需要请求一次身份验证...如果用户授予了权限,则无需再次询问,如果用户拒绝,则无法再次提示。 you need to handle it differently if denied. 如果遭到拒绝,您需要采取不同的处理方式。 you push the user to the app settings in the settings menu and the user has to enable from there 您将用户推送到设置菜单中的应用程序设置,用户必须从那里启用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM