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.")
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With