如果用户未批准位置,如何在每次打开视图时请求访问位置的权限

时间:2019-09-21 19:07:55

标签: ios swift location-services

我正在尝试打开一个需要访问用户位置的视图控制器。目前,我正在运行以下代码。但是,这仅在用户首次打开视图时要求用户许可。之后,如果用户拒绝,则应用程序将崩溃。

我在viewDidLoad()的开头运行了此函数,然后运行

centerMapOnLocation(CLLocationManager().location!, mapView: mapView)

此行代码将在其中崩溃并显示错误消息:

  

线程1:致命错误:在展开可选值时意外发现nil

是什么原因造成的?

func askForPermission() {
    // Ask for Authorisation from the User.
    self.locationManager.requestAlwaysAuthorization()

    // For use in foreground
    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() == true {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }
    else {
        let alertController = UIAlertController(title: "Location Service Error", message: "Go to settings to approve Location services", preferredStyle: .alert)

        // Create OK button
        let OKAction = UIAlertAction(title: "Ok", style: .default) { (action:UIAlertAction!) in
            UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)

        }
        alertController.addAction(OKAction)

        // Create Cancel button
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
            self.dismiss(animated: true, completion: nil)
        }
        alertController.addAction(cancelAction)

        // Present Dialog message
        self.present(alertController, animated: true, completion:nil)
    }
}

我也试图运行以下功能,并在同一位置遇到崩溃。

func checkAuthorizationStatus() {
    switch CLLocationManager.authorizationStatus() {
    case .authorizedWhenInUse:
        mapView.showsUserLocation = true
        break
    case .denied:
        let alertController = UIAlertController(title: "Location Service Error", message: "Go to settings to approve Location services", preferredStyle: .alert)

        // Create OK button
        let OKAction = UIAlertAction(title: "Ok", style: .default) { (action:UIAlertAction!) in
            UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)

        }
        alertController.addAction(OKAction)

        // Create Cancel button
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
            self.dismiss(animated: true, completion: nil)
        }
        alertController.addAction(cancelAction)

        // Present Dialog message
        self.present(alertController, animated: true, completion:nil)
        break
    case .notDetermined:
        locationManager.requestWhenInUseAuthorization()
        mapView.showsUserLocation = true
        break
    case .restricted:
        let alertController = UIAlertController(title: "Location Service Error", message: "Go to settings to approve Location services", preferredStyle: .alert)

        // Create OK button
        let OKAction = UIAlertAction(title: "Ok", style: .default) { (action:UIAlertAction!) in
            UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)

        }
        alertController.addAction(OKAction)

        // Create Cancel button
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
            self.dismiss(animated: true, completion: nil)
        }
        alertController.addAction(cancelAction)

        // Present Dialog message
        self.present(alertController, animated: true, completion:nil)
        break
    case .authorizedAlways: break
    }
}

我该如何解决?

1 个答案:

答案 0 :(得分:-1)

您可以通过为特定功能分配任何特定的“按钮动作”或“某些活动”来实现它,

`onClick(视图c){

//在这里触发您的代码

func askForPermission(){....}

}`

另外,经典的 Try catch 语句可以很好地防止应用崩溃,或者您可以为该特定事件使用其他线程。