代码之家  ›  专栏  ›  技术社区  ›  Adam

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

  •  -1
  • Adam  · 技术社区  · 6 年前
    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{
            }
    

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

    但如果我否认,这是不会弹出下次。

    我创建按钮

    运行此代码

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

    但不起作用

    3 回复  |  直到 6 年前
        1
  •  13
  •   Ehsan Saddique    4 年前

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

        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 .

    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)
    }
    
        2
  •  1
  •   SAXENA    6 年前

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

     /*  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"
    
    }
    
        3
  •  1
  •   Keshu R.    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)
    }
    
        4
  •  0
  •   kathayatnk    6 年前

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

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