Bernd
Bernd

Reputation: 11493

Identify "Not Determined" case for Local Notifications settings

As of iOS8 you to need to register and prompt the user for the use of Local Notifications. Therefore I'd like to implement a way to double check these permissions.

How can I check for the case of Local Notifications settings being not determined/not set yet? So far I only know how to check for Local Notifications being granted or denied, like this …

    var currentStatus = UIApplication.sharedApplication().currentUserNotificationSettings()
    var requiredStatus:UIUserNotificationType = UIUserNotificationType.Alert

    if currentStatus.types == requiredStatus {
     … //GRANTED
     } else {
     … //DENIED
     }

The problem using this is I also get a Denied if there is nothing set so far. How can I differentiate all 3 cases?

As an alternative solution it would be helpful to have a comparable delegate method to CoreLocation's authorization didChangeAuthorizationStatus in order to react on the user's selection on the permissions alert. Is there something like this in order to get the state of user interaction with the privacy alert for local notifications?

Upvotes: 6

Views: 530

Answers (2)

Ambroise Collon
Ambroise Collon

Reputation: 3869

The solution I implemented :

In app delegate, I detect when didRegisterUserNotificationSettings is shot. And I save in userdefaults a bool to true :

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "notificationsDeterminedKey")
        NSUserDefaults.standardUserDefaults().synchronize()
}

And when I need to know the status :

if NSUserDefaults.standardUserDefaults().boolForKey("notificationsDeterminedKey") {
    let grantedSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
    if grantedSettings.types == UIUserNotificationType.None {
        // Denied
    } else {
        // Granted
} else {
    // Not Determined
}

Upvotes: 2

Bernd
Bernd

Reputation: 11493

I just found an appropriate UIApplication delegate method that helps to solve this:

- (void)application:(UIApplication *)application
    didRegisterUserNotificationSettings:
    (UIUserNotificationSettings *)notificationSettings {

You can find more details on this in WWDC14 Session 713 "What’s New in iOS Notifications".

Upvotes: 1

Related Questions