Request permissions again after user abandons location services?

I track the location of the user and request permission when my download first loads using this:

locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() 

If the user denies, but later changes his mind by turning on the configuration option in my application, how can I ask again? For example, I have a switch to automatically detect the user's location, so when they activate it, I try to do this:

 @IBAction func gpsChanged(sender: UISwitch) { // Request permission for auto geolocation if applicable if sender.on { locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() } } 

But this code does not seem to do anything. I was hoping he would ask the user again if they want the application to track the user's location. Is it possible?

+24
source share
5 answers

The OS will ask the user only once. If they refuse permission, then this. What you can do is directly use the user settings for your application by passing the UIApplicationOpenSettingsURLString to UIApplication openURL: method. From there, they can re-enable location services if they so wish. However, you probably shouldn't be too aggressive in listening to them for permission.

+53
source

The permission popup is displayed only once. Therefore, we must redirect users to Settings after this. Here is 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) } } 
+12
source

You may have an alternative solution! You can show your own warning with the best message, which can convince your user to allow push notifications for your application. If the user allows, then only you show the default permission warning to enable push notification; otherwise, if the user forbids not to display the default warning in reality, you can save the corresponding flag in your database or NSUserDefaults and you can ask the user again and again some events in your application.

+3
source

You get only one chance. In order for the user to allow permissions after their refusal, they must go through the Settings application. See Requesting permission to use location services in the CLLocationManager .

+1
source

I created a library that includes a notification permission manager that handles permission notification even after a user has denied permission.

https://github.com/CrazyPro007/PermissionManager/tree/master/PermissionManager/PermissionManager

+1
source

Source: https://habr.com/ru/post/986338/


All Articles