Check iOS8 notification settings

I am trying to verify that the user has authorized alerts and icons. I had a problem figuring out what to do with the current settings when I receive it.

let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
println(settings)
// Prints - <UIUserNotificationSettings: 0x7fd0a268bca0; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);>

if settings.types == UIUserNotificationType.Alert // NOPE - this is the line that needs an update
{
    println("Yes, they have authorized Alert")
}
else
{
    println("No, they have not authorized Alert.  Explain to them how to set it.")
}
+4
source share
1 answer

You check with a help ==that will only return true if all the set parameters are contained in the value you are comparing. Remember that this is a bitmap rename where you add additional parameters to the same value with a bitwise or |. You can check if a particular option is part of a different value &.

if settings.types & UIUserNotificationType.Alert != nil {
    // .Alert is one of the valid options
}

Swift 2.0+ . [UIUserNotificationType], :

if settings.types.contains(.Alert) {
   // .Alert is one of the valid options
}
+7

All Articles