Swift 检查iOS8的通知设置

Swift 检查iOS8的通知设置,swift,ios8,Swift,Ios8,我正在尝试检查以确保用户拥有授权的警报和徽章。一旦我得到当前设置,我就很难弄清楚如何处理它 let settings = UIApplication.sharedApplication().currentUserNotificationSettings() println(settings) // Prints - <UIUserNotificationSettings: 0x7fd0a268bca0; types: (UIUserNotificationTypeAlert UIUserN

我正在尝试检查以确保用户拥有授权的警报和徽章。一旦我得到当前设置,我就很难弄清楚如何处理它

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.")
}
let settings=UIApplication.sharedApplication().currentUserNotificationSettings()
println(设置)
//印刷品-
如果settings.types==UIUserNotificationType.Alert//NOPE-这是需要更新的行
{
println(“是的,他们有授权警报”)
}
其他的
{
println(“不,他们没有授权警报。向他们解释如何设置它。”)
}

您正在使用
=
进行检查,只有当所有设置选项都包含在您要比较的值中时,才会返回true。请记住,这是一个位图枚举,您可以使用按位或
|
向同一值添加其他选项。您可以通过按位
&
检查特定选项是否是值的一部分

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
}

非常感谢。工作起来很有魅力。