iOS Swift如何打开位置权限弹出窗口

Posted

技术标签:

【中文标题】iOS Swift如何打开位置权限弹出窗口【英文标题】:iOS Swift How to open Location Permission popup 【发布时间】:2019-02-18 01:01:06 【问题描述】:
var locMgr = INTULocationManager.sharedInstance()
    locMgr.requestLocation(withDesiredAccuracy: .city, timeout: 30, delayUntilAuthorized: true,block: (currentLoc: CLLocation!, achievedAccuracy: INTULocationAccuracy, status: INTULocationStatus) -> Void in
        if status == INTULocationStatus.success 
        
        else
        

使用过 INTULocationManager , Swift 4.1 , ios 11.1

如果第一次运行此代码会弹出位置权限请求

但是如果我拒绝了,下次就不会弹出这个了。

如何打开权限弹窗?

我创建按钮

运行这段代码

let locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()

但没用

【问题讨论】:

【参考方案1】:

一旦用户拒绝权限,没有任何默认功能会弹出位置权限。您需要向用户显示需要权限的警报,然后将用户重定向到设置屏幕。 这是您可以使用的完整代码。 定义一个检查位置权限的函数。

    func hasLocationPermission() -> Bool 
        var hasPermission = false
        if CLLocationManager.locationServicesEnabled() 
            switch CLLocationManager.authorizationStatus() 
            case .notDetermined, .restricted, .denied:
                hasPermission = false
            case .authorizedAlways, .authorizedWhenInUse:
                hasPermission = true
            
         else 
            hasPermission = false
        
        
        return hasPermission
    

现在通过此功能检查位置权限,并在需要时显示警报。

    if !hasLocationPermission() 
            let alertController = UIAlertController(title: "Location Permission Required", message: "Please enable location permissions in settings.", preferredStyle: UIAlertControllerStyle.alert)
            
            let okAction = UIAlertAction(title: "Settings", style: .default, handler: (cAlertAction) in
                //Redirect to Settings app
                UIApplication.shared.open(URL(string:UIApplicationOpenSettingsURLString)!)
            )
            
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel)
            alertController.addAction(cancelAction)
            
            alertController.addAction(okAction)
            
            self.present(alertController, animated: true, completion: nil)
        

别忘了导入CoreLocation

Swift 5.3 和 iOS 14 版本

func hasLocationPermission() -> Bool 
    var hasPermission = false
    let manager = CLLocationManager()
    
    if CLLocationManager.locationServicesEnabled() 
        switch manager.authorizationStatus 
        case .notDetermined, .restricted, .denied:
            hasPermission = false
        case .authorizedAlways, .authorizedWhenInUse:
            hasPermission = true
        @unknown default:
                break
        
     else 
        hasPermission = false
    
    
    return hasPermission


if !hasLocationPermission() 
    let alertController = UIAlertController(title: "Location Permission Required", message: "Please enable location permissions in settings.", preferredStyle: .alert)
    
    let okAction = UIAlertAction(title: "Settings", style: .default, handler: (cAlertAction) in
        //Redirect to Settings app
        UIApplication.shared.open(URL(string:UIApplication.openSettingsURLString)!)
    )
    
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
    alertController.addAction(cancelAction)
    
    alertController.addAction(okAction)
    
    self.present(alertController, animated: true, completion: nil)

【讨论】:

导入核心位置 'authorizationStatus()' 在 iOS 14.0 中已弃用【参考方案2】:

斯威夫特 5

一旦用户拒绝该权限,警报就会对您的应用禁用,并且不会再次显示。 您可以向用户显示需要权限的弹出窗口。

这是您可以使用的完整代码

  if CLLocationManager.locationServicesEnabled() 
        switch CLLocationManager.authorizationStatus() 
        case .notDetermined, .restricted, .denied:
            showPermissionAlert()
        case .authorizedAlways, .authorizedWhenInUse:
            locationManager.startUpdatingLocation()
        
     else 
       locationManager.startUpdatingLocation()
    

现在通过此功能检查位置权限,并在需要时显示警报。

   func showPermissionAlert()
    let alertController = UIAlertController(title: "Location Permission Required", message: "Please enable location permissions in settings.", preferredStyle: UIAlertController.Style.alert)

    let okAction = UIAlertAction(title: "Settings", style: .default, handler: (cAlertAction) in
        //Redirect to Settings app
        UIApplication.shared.open(URL(string:UIApplication.openSettingsURLString)!)
    )

    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel)
    alertController.addAction(cancelAction)

    alertController.addAction(okAction)

    self.present(alertController, animated: true, completion: nil)

【讨论】:

【参考方案3】:

如果权限被用户拒绝,则打开权限弹出窗口

 /*  func checkLocation() 
    if CLLocationManager.authorizationStatus() != .authorizedWhenInUse
    
        print("requesting autorization")
        locationManager.requestWhenInUseAuthorization()

     else 
        print("start updating location")
    
*/

func askEnableLocationService() ->String 
    var showAlertSetting = false
    var showInitLocation = false
    if CLLocationManager.locationServicesEnabled() 
        switch CLLocationManager.authorizationStatus() 
        case .denied:
            showAlertSetting = true
            print("HH: kCLAuthorizationStatusDenied")
        case .restricted:
            showAlertSetting = true
            print("HH: kCLAuthorizationStatusRestricted")
        case .authorizedAlways:
            showInitLocation = true
            print("HH: kCLAuthorizationStatusAuthorizedAlways")
        case .authorizedWhenInUse:
            showInitLocation = true
            print("HH: kCLAuthorizationStatusAuthorizedWhenInUse")
        case .notDetermined:
            showInitLocation = true
            print("HH: kCLAuthorizationStatusNotDetermined")
        default:
            break
        
    else
        showAlertSetting = true
        print("HH: locationServicesDisabled")

    
    if showAlertSetting 
        let alertController = UIAlertController(title: "xxxxxx", message: "Please enable location service in the settings", preferredStyle: .alert)
        let OKAction = UIAlertAction(title: "OK", style: .default)  (action:UIAlertAction!) in



            if let url = URL(string: UIApplicationOpenSettingsURLString) 
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            

        
        alertController.addAction(OKAction)
        self.window?.rootViewController?.present(alertController, animated: true, completion:nil)

    
    if showInitLocation 

        return "YES"

    
    return "NO"


【讨论】:

这在 OP 描述的情况下不起作用,其中权限对话框已经显示并且权限被用户拒绝。 @Paulw11 检查此功能它会自动重定向设置。 是的,您编辑的代码会,但它与已经接受的答案基本相同【参考方案4】:

这是默认行为。首次显示弹出窗口后。随后的请求将被视为拒绝或第一次选择时选择的任何内容。但是,您可以实现自己的警报并将用户直接发送到设置应用程序以授予位置访问权限,如下所示:

//check if user has denied the access on first popup
    if !permissionGranted 

        let permissionAlert = UIAlertController(title: "Location Access", message: "Requires location access to take advantage of this feature. Please provide location access from settings", preferredStyle: .alert)

        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
        let settingAction = UIAlertAction(title: "Settings", style: .default)  (action) in
            guard let appSettingURl = URL(string: UIApplicationOpenSettingsURLString) else  return 
            if UIApplication.shared.canOpenURL(appSettingURl) 
                UIApplication.shared.open(appSettingURl, options: [:], completionHandler: nil)
            
        
        permissionAlert.addAction(cancelAction)
        permissionAlert.addAction(settingAction)
        present(permissionAlert, animated: true, completion: nil)
    

【讨论】:

【参考方案5】:

Complete Solution : (iOS 14+ and also for prior versions)

1)首先获取授权状态:-

func locationAuthorizationStatus() -> CLAuthorizationStatus 
        let locationManager = CLLocationManager()
        var locationAuthorizationStatus : CLAuthorizationStatus
        if #available(iOS 14.0, *) 
            locationAuthorizationStatus =  locationManager.authorizationStatus
         else 
            // Fallback on earlier versions
            locationAuthorizationStatus = CLLocationManager.authorizationStatus()
        
        return locationAuthorizationStatus
    

2)然后检查位置权限:-

func hasLocationPermission() -> Bool 
        var hasPermission = false
        let manager = self.locationAuthorizationStatus()
        
        if CLLocationManager.locationServicesEnabled() 
            switch manager 
            case .notDetermined, .restricted, .denied:
                hasPermission = false
            case .authorizedAlways, .authorizedWhenInUse:
                hasPermission = true
            @unknown default:
                    break
            
         else 
            hasPermission = false
        
        
        return hasPermission
    

3)然后显示警报

if !hasLocationPermission() 
    let alertController = UIAlertController(title: "Location Permission Required", message: "Please enable location permissions in settings.", preferredStyle: .alert)
    
    let okAction = UIAlertAction(title: "Settings", style: .default, handler: (cAlertAction) in
        //Redirect to Settings app
        UIApplication.shared.open(URL(string:UIApplication.openSettingsURLString)!)
    )
    
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
    alertController.addAction(cancelAction)
    
    alertController.addAction(okAction)
    
    self.present(alertController, animated: true, completion: nil)

干杯 :) :)

【讨论】:

以上是关于iOS Swift如何打开位置权限弹出窗口的主要内容,如果未能解决你的问题,请参考以下文章

在 Swift 3 中单击标签栏项目时从底部打开弹出窗口?

位置权限弹出窗口

出现“使用当前位置”权限弹出窗口时,Appium 和 Selenium 无法连接

如何在 MIUI 中打开 Draw Overlay 权限弹出窗口?

在 iPhone 应用程序中更改系统弹出窗口以获得核心位置权限

如何使用 iOS 的 phonegap 在弹出窗口中打开 PDF 文件