微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.")
}

解决方法

您正在使用==进行检查,如果所有设置选项都包含在您要比较的值中,则只会返回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
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐