Request permissions again after user denies location services?
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)
}
}
You can have an alternate solution!! You can show your own alert with the better message which can convince your user to allow to receive push notifications for your app. If user allows, then only you show default permission alert for enable push notification otherwise if user disallows, don't show default alert in-fact, you can save corresponding flag in your database or NSUserDefaults and can ask user later on again and again on some events in your app.
The OS will only ever prompt the user once. If they deny permission, that's it. What you can do is direct the user to the Settings for your app by passing UIApplicationOpenSettingsURLString
to UIApplication
's openURL:
method. From there, they can re-enable location services if they wish. That said, you probably shouldn't be too aggressive about bugging them for the permission.