检查通知设置iOS8

Sha*_*DES 4 swift ios8

我正在尝试检查以确保用户已授权警报和徽章.一旦得到它,我在弄清楚如何处理当前设置时遇到了问题.

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.")
}
Run Code Online (Sandbox Code Playgroud)

Mun*_*ndi 7

==如果所有设置选项都包含在您要比较的值中,那么您正在检查哪个只会返回true.请记住,这是一个位图枚举,您可以使用按位或向其添加其他选项到相同的值|.您可以使用按位检查特定选项是否是值的一部分&.

if settings.types & UIUserNotificationType.Alert != nil {
    // .Alert is one of the valid options
}
Run Code Online (Sandbox Code Playgroud)

在Swift 2.0+中,您需要使用新的表示法.您的设置集合是一个类型的数组[UIUserNotificationType],因此您检查如下:

if settings.types.contains(.Alert) {
   // .Alert is one of the valid options
}
Run Code Online (Sandbox Code Playgroud)