gpt4 book ai didi

ios - 为什么 UIUserNotificationType.None 在给定用户权限时在当前设置中返回 true?

转载 作者:可可西里 更新时间:2023-11-01 03:57:54 25 4
gpt4 key购买 nike

我正在编写一种方法来检查当前用户设置是否包含某些通知类型。

当检查当前设置是否包含 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

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;

关于ios - 为什么 UIUserNotificationType.None 在给定用户权限时在当前设置中返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32248411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com