How do I interrogate UIUserNotificationType returned from UIApplication.sharedApplication().currentUserNotificationSettings().types

For the life of me I cannot figure this out....


How do I interrogate UIUserNotificationType returned from UIApplication.sharedApplication().currentUserNotificationSettings().types?


UIUserNotificationType is a RawOptionSetType

UIUserNotificationType.None

UIUserNotificationType.Badge

UIUserNotificationType.Sound

UIUserNotificationType.Alert]

It seems that every possible combination or permutation of the 4 values can be returned in the types variable based on whatever the current App notification settings are?

I just want to interrogate or query the returned types variable and see if UIUserNotificationType.Sound is turned on/off or true/false.

I don't want to resort to using currentUserNotificationSettings().hash and guess at getting the bits for each compination of values!

Below is the ugly / scary way to do it from Stackoverflow, I don't want to do that!

[[UIApplication sharedApplication] currentUserNotificationSettings].hash.


I believe that this returns a three bit hash value where bit one is for Banner notifications, bit 2 is for Sound notifications, and bit 3 is for Alert notifications.


So...


000 = 0 = no notifications.

001 = 1 = only banner,

010 = 2 = only sound,

011 = 3 = sound and banner, no alert

100 = 4 = only alert notifications and so on until,

111 = 7 = all notifications on.
Accepted Answer

I assume you are using Xcode 6.4 as you say UIUserNotificationType as RawOptionSetType.


It seems that every possible combination or permutation of the 4 values can be returned in the types variable

The types property, of UIUserNotificationType, contains every possible combination of 3 values -- .Alert, .Badge and .Sound .

.None represents the combination where none of the three values included.


In Swift 1.2, bitwise operators are defined for RawOptionSetTypes, so you can check each element using C-like notation:

        let notificationType = UIApplication.sharedApplication().currentUserNotificationSettings()!.types
        if notificationType == UIUserNotificationType.None {
            println("None")
        } else {
            if notificationType & .Alert != .allZeros {
                print("Alert")
            }
            if notificationType & .Badge != .allZeros {
                print("Badge")
            }
            if notificationType & .Sound != .allZeros { //UIUserNotificationType.Sound is turned on
                print("Sound")
            }
            println()
        }


Remember, in Swift 2, RawOptionSetType is replaced by OptionSetType, so you need to rewrite all this sort of checking codes in Swift 2.

Thanks, that really helped me!


I was focused on 4 when there really were only 3. None is none! Also the iPhone Settings notification screen can be kind of confusing having Sounds, Badges, Banners, and Alerts!


Yes, I am still on Swift 1.2 but I will be switching to Swift 2.0 very soon! I am guessing that the non-beta version of Xcode 7 will be released next week with all of the shiny new objects! :-)


Thanks again, I really appreciate your help!


Where did .allZeros come from? I had been using .None which had been seeming to work!

Where did .allZeros come from? I had been using .None which had been seeming to work!

The allZeros property is defined in BitwiseOperationsType which is inherited by RawOptionSetType. In all (as far as I know) RawOptionSetTypes, .None is exactly the same as BitwiseOperationsType.allZeros, so you can use any of them as you prefer. I tend to use allZeros where bitwise operations included.

How do I interrogate UIUserNotificationType returned from UIApplication.sharedApplication().currentUserNotificationSettings().types
 
 
Q