Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking the Notification Settings iOS8

Tags:

swift

ios8

I'm trying to check to make sure that the user has authorized alerts and badges. I'm having a problem figuring out what to do with the current settings once I get 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.")
}
like image 813
ShadowDES Avatar asked Mar 19 '23 07:03

ShadowDES


1 Answers

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

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

In Swift 2.0+ you need to use the new notation. Your settings collection is an array of type [UIUserNotificationType], so you check like this:

if settings.types.contains(.Alert) {
   // .Alert is one of the valid options
}
like image 107
Mundi Avatar answered Mar 27 '23 12:03

Mundi