Request permissions again after user denies location services?

后端 未结 6 1597
耶瑟儿~
耶瑟儿~ 2020-12-05 02:43

I track the user\'s location and ask for permission when my load first loads using this:

locationManager.requestAlwaysAuthorization()
locationManager.startUp         


        
6条回答
  •  眼角桃花
    2020-12-05 03:06

    The permission pop up only shows once. So we have to redirect users to Settings after that. Here comes the code in Swift:

     import CoreLocation 
     @IBAction func userDidClickButton(_ sender: Any) {
    
        // initialise a pop up for using later
        let alertController = UIAlertController(title: "TITLE", message: "Please go to Settings and turn on the permissions", preferredStyle: .alert)
        let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
            guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
                return
            }
            if UIApplication.shared.canOpenURL(settingsUrl) {
                UIApplication.shared.open(settingsUrl, completionHandler: { (success) in })
             }
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
        alertController.addAction(cancelAction)
        alertController.addAction(settingsAction)
    
        // check the permission status
        switch(CLLocationManager.authorizationStatus()) {
            case .authorizedAlways, .authorizedWhenInUse:
                print("Authorize.")
                // get the user location
            case .notDetermined, .restricted, .denied:
                // redirect the users to settings
                self.present(alertController, animated: true, completion: nil)
        }
    }
    

提交回复
热议问题