我正在编写一种方法来检查当前用户设置是否包含某些通知类型。
当检查当前设置是否包含 UIUserNotificationsType.None 时,无论授予权限还是拒绝权限,它都会返回 true。有谁知道这是为什么吗?
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: 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 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 中更清晰可见:
typedef enum UIUserNotificationType : NSUInteger {
UIUserNotificationTypeNone = 0,
UIUserNotificationTypeBadge = 1 << 0,
UIUserNotificationTypeSound = 1 << 1,
UIUserNotificationTypeAlert = 1 << 2,
} UIUserNotificationType;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)