当授予用户权限时,为什么UIUserNotificationType.None在当前设置中返回true? [英] Why does UIUserNotificationType.None return true in the current settings when user permission is given?

查看:472
本文介绍了当授予用户权限时,为什么UIUserNotificationType.None在当前设置中返回true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一种方法来检查当前用户设置是否由某些通知类型组成.

I'm writing a method to check if the current user settings consist of certain notification types.

当检查当前设置是否包含UIUserNotificationsType.None时,在授予和拒绝权限时均返回true.谁知道这是为什么吗?

When checking whether the current settings contain UIUserNotificationsType.None, it returns true for both when the permission was given and denied. Would anyone know why this is?

func registerForAllNotificationTypes()
{
    registerNotificationsForTypes([.Badge, .Alert, .Sound])
}

func registerNotificationsForTypes(types:UIUserNotificationType)
{
    let settings = UIUserNotificationSettings.init(forTypes:types, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}

func isRegisteredForAnyNotifications() -> Bool
{
    let currentSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
    print(currentSettings)
    print((currentSettings?.types.contains(.Alert))!)
    print((currentSettings?.types.contains(.Badge))!)
    print((currentSettings?.types.contains(.Sound))!)
    print((currentSettings?.types.contains(.None))!)

    return (currentSettings?.types.contains(.Alert))! //Just testing .Alert for now
}

启用权限后:

Optional(<UIUserNotificationSettings: 0x7fabdb719360; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);>) true true true true

Optional(<UIUserNotificationSettings: 0x7fabdb719360; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);>) true true true true

关闭权限后:

Optional(<UIUserNotificationSettings: 0x7f96d9f52140; types: (none);>) false false false true

Optional(<UIUserNotificationSettings: 0x7f96d9f52140; types: (none);>) false false false true

推荐答案

有趣的是,它只是确认0包含0 :) 看一下UIUserNotificationsType的枚举定义: https ://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIUserNotificationSettings_class/index.html#//apple_ref/c/tdef/UIUserNotificationType

Funny thing, it just confirms that 0 contains 0 :) Take a look on enum definition for UIUserNotificationsType: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIUserNotificationSettings_class/index.html#//apple_ref/c/tdef/UIUserNotificationType

struct UIUserNotificationType : OptionSetType {
    init(rawValue rawValue: UInt)
    static var None: UIUserNotificationType { get }
    static var Badge: UIUserNotificationType { get }
    static var Sound: UIUserNotificationType { get }
    static var Alert: UIUserNotificationType { get }
}

但是在Objective-C中更清晰可见:

But it's more clearly visible in Objective-C:

typedef enum UIUserNotificationType : NSUInteger {
   UIUserNotificationTypeNone    = 0,
   UIUserNotificationTypeBadge   = 1 << 0,
   UIUserNotificationTypeSound   = 1 << 1,
   UIUserNotificationTypeAlert   = 1 << 2,
} UIUserNotificationType;

这篇关于当授予用户权限时,为什么UIUserNotificationType.None在当前设置中返回true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆