【问题标题】:Why does UIUserNotificationType.None return true in the current settings when user permission is given?为什么 UIUserNotificationType.None 在授予用户权限时在当前设置中返回 true?
【发布时间】:2015-08-27 19:51:07
【问题描述】:

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

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

【问题讨论】:

  • 我觉得它的行为正常(考虑到它的实现方式),但感觉就像一个错误,因为 .None 具有误导性。如果.None 总是正确的,那我们为什么要从它开始呢?

标签: ios swift2


【解决方案1】:

有趣的是,它只是确认 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;

【讨论】:

  • 我明白了。然而,在我的脑海中,我希望单独使用 contains api 就足以检查用户是否没有授予权限。我想知道这是否是已知/可接受的行为
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 2019-04-05
  • 2010-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多