10

I want ask permission in the second time in my home controller, can I do it programmatically?

I mean that the user disable it in the first time and I want to allow him another option to get notification.

Hen Shabat
  • 519
  • 1
  • 6
  • 19

5 Answers5

16

You´re not allowed to do that. The notification popup will prompt the first time the user opens the application. What you can do is check if the user hasn´t allowed this. Then you could open the settings page (which basically is what you can do in this case):

let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if !isRegisteredForRemoteNotifications {
    UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)
}

Swift 5.x

let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if !isRegisteredForRemoteNotifications {
    UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
}
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
9

This is actually not possible. You only get one shot to prompt them for permissions. This is why most apps will present a custom view to explain why a certain permission is needed. And if the user clicks "yes", then they launch the actual permission alert. If they have already declined the permission, you'll need to check if app has certain permission and prompt them to go into settings to activate what is needed.

Here's an example of how you can check if they have given permission.

valosip
  • 3,167
  • 1
  • 14
  • 26
7

You cannot ask for a permission after the user has chosen to allow it or not. What you can do is check if the permission was not allowed and redirect the user to the app's settings.

How you check for the permission's authorization status depends on the type of service you want to authorize. You can the redirect the user to the settings with the following code:

Swift

UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil)

Objective C

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
Julius
  • 1,005
  • 1
  • 9
  • 19
5

You cannot show the standard popup after the user enabled or refused notifications. In such situation, it is quite common to show an alertController that informs the user about this situation and provide her with a button that navigates to settings:

let alert = UIAlertController(title: "Unable to use notifications",
                              message: "To enable notifications, go to Settings and enable notifications for this app.",
                              preferredStyle: UIAlertControllerStyle.alert)

let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(okAction)

let settingsAction = UIAlertAction(title: "Settings", style: .default, handler: { _ in
    // Take the user to Settings app to possibly change permission.
    guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else { return }
    if UIApplication.shared.canOpenURL(settingsUrl) {
        UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
            // Finished opening URL
        })
    }
})
alert.addAction(settingsAction)

self.present(alert, animated: true, completion: nil)

The code is inspired by a similar example for camera access by Apple engineers.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
1

In such scenario, you can show an alert which navigates user to settings

let alert = UIAlertController(title: "Allow notification Access", message: "Allow notification access in your device settings.", preferredStyle: UIAlertController.Style.alert)

// Button to Open Settings
alert.addAction(UIAlertAction(title: "Settings", style: UIAlertAction.Style.default, handler: { action in
                            guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
                                return
                            }
                            if UIApplication.shared.canOpenURL(settingsUrl) {
                                UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                                    print("Settings opened: \(success)")
                                })
                            }
                        }))
    alert.addAction(UIAlertAction(title: "Close", style: UIAlertAction.Style.default, handler: nil))
    self.present(alert, animated: true, completion: nil)
Kishan Bhatiya
  • 2,175
  • 8
  • 14