加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 检查通知设置iOS8

发布时间:2020-12-14 04:34:59 所属栏目:百科 来源:网络整理
导读:我正在尝试检查以确保用户已授权警报和徽章.一旦得到它,我在弄清楚如何处理当前设置时遇到了问题. let settings = UIApplication.sharedApplication().currentUserNotificationSettings()println(settings)// Prints - UIUserNotificationSettings: 0x7fd0a2
我正在尝试检查以确保用户已授权警报和徽章.一旦得到它,我在弄清楚如何处理当前设置时遇到了问题.

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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读