为什么在授予用户权限时,UIUserNotificationType.None 在当前设置中返回 true?

2024-05-18

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

当检查当前设置是否包含 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(使用前将#替换为@)

为什么在授予用户权限时,UIUserNotificationType.None 在当前设置中返回 true? 的相关文章

  • 如何修复C风格的for语句?

    什么是正确的修复方法C 风格的 for 语句对于下面发布的代码 目前我正在交战 C 风格的 for 语句已弃用 并将在将来删除 斯威夫特的版本 var ifaddr UnsafeMutablePointer
  • 移动 Safari 中的文件上传和 EXIF

    正如这些问题中所指出的 在某些情况下 iOS 上上传的照片的地理位置和其他 EXIF 元数据会被删除 在 safari 中 https apple stackexchange com questions 326789 gps exif fr
  • UITextField 字体大小在开始或停止输入字符时发生变化

    我见过很多线程 但没有找到解决这个问题的任何具体解决方案 我使用 XIB 创建了 UITextfield 尚未以编程方式对该字段执行任何操作 一旦我开始打字 文本字段的字体就会改变 一旦我将焦点移出文本字段 并且随着键盘消失 字体大小就会减
  • 检测您何时进入/退出 Xamarin.iOS 中的主线程

    Xamarin MonoTouch 有没有办法检测主线程中是否正在调用代码 我正在寻找类似于Java的东西EventQueue isEventDispatchThread 我发现 Swing 编程很方便assert时不时 或有时assert
  • iOS UITableViewCell 配件在左侧?

    对于我的应用程序 我想要一些可以同时具有复选标记和详细信息披露按钮的单元格 也就是说 我希望它们看起来与 iOS 设置中的 Wi Fi 网络选择一模一样 左侧的复选标记 中间的内容 右侧的详细信息披露按钮 有没有正确的方法来做到这一点 或者
  • 使用 Parse.com 上传视频

    我是解析新手 正在尝试保存视频并将其上传到云端 这是我正在使用的代码 但是当调用 didButtonAction 时 它不断收到错误 我相信问题出在将视频保存为文件时 但我不知道如何解决这个问题 先感谢您 void imagePickerC
  • 删除后台 moc 中的对象然后在主 moc 中刷新它会导致 NSFetchedResultsController 更新崩溃

    我遇到了一个NSObjectInaccessibleException我无法理解 Terminating app due to uncaught exception NSObjectInaccessibleException reason
  • Swift 中的弹出视图

    我有一个弹出视图 没有选项卡栏 它弹出到带有选项卡栏的视图控制器 在带有选项卡栏的视图控制器中 我设置了一个单击按钮 以便弹出视图控制器 IBAction func PopUpClicked sender UIButton gt Void
  • 应用程序图标未刷新

    我更改了新版本应用程序中的图标图像 并且我在设备中安装了旧版本应用程序 然后我安装了新版本 它在 iOS 5 中运行良好 但在 iOS 6 中 图标没有刷新 它仍然显示旧版本图标 徽标 如果没有安装旧版本应用程序 该设备在 iOS 5 和
  • 从 Plist 中存储和检索 [重复]

    这个问题在这里已经有答案了 可能的重复 iOS 在 plist 文件中存储两个 NSMutableArray https stackoverflow com questions 6070568 ios store two nsmutable
  • 对 UIImage 进行方形裁剪,导致图像拉伸

    当尝试执行 UIImage 的中心裁剪时 我得到以下结果 左侧是原始图像640 1136 右边是适合正方形的裁剪图像UIImageView at 320 320 turns to 我对比率元素进行了相当多的修改 以便它可以正确检测要修剪的量
  • iOS:从非图像数据生成图像(Godus,如风景)

    所以看到图像后Godus http www kickstarter com projects 22cans project godus我想知道如何生成简单的 非交互式的 2D 图像 with 不同高度或层数的颜色不同就像下面的图片一样 我只
  • 尝试从独立的 Apple Watch 应用发出网络请求

    当应用程序是独立应用程序时 Apple Watch 是否无法进行网络通话 即使手表已连接到 iPhone 我正在使用新的独立应用程序目标 它没有附带可以发出 WatchConnectivity 请求的配对 iOS 应用程序 我十有八九收到
  • 从 Core Data、iOS 获取最后插入的项目

    有没有办法获取核心数据数据库中最后插入的项目 这将返回最后插入的对象 setFetchLimit 1 和 setFetchOffset 所有条目数 1
  • swift 中的负数模

    负数模如何在 swift 中工作 当我执行 1 3 时 它给出 1 但余数是 2 其中有什么问题 雨燕余数运算符 计算余数 整数除法 a b a a b b where 是截断整数除法 在你的情况下 1 3 1 1 3 3 1 0 3 1
  • iPad 3 中配备 Xcode 4.2 和 Retina 的 iOS 5.1

    我有一台装有 Mac OS X Snow Leopard 的 Mac 我可以添加 iOS 5 1 吗 使用 iPad 3 的新分辨率 我们将如何处理图像 因为如果该应用程序将在 iPhone 3GS 4 和 iPad 3 中运行 我认为我们
  • 在 Swift 中检查一个数组是否包含另一个数组的所有元素

    我想为数组编写一个扩展来检查一个数组是否包含另一个数组的所有元素 在我的用例中它是字符串对象 但我一直得到 Cannot convert value of type T Generator Element to expected argum
  • Swift 中 UIImages 的淡入淡出动画

    我有一组图像 我希望它们在登录屏幕的背景中淡出 我无法在 swift 中找到任何可以做到这一点的东西 有什么办法我可以做到吗 这是我当前的代码 override func viewDidLoad super viewDidLoad star
  • 如何自动为 Swift 类创建初始化程序?

    UPDATE 使用结构而不是类 struct 在很多方面都更好 它有自己的初始化器 这是我的模型课 是否有可能创建init自动方法 每次我都必须将所有变量一一初始化 这会花费很多时间 class Profile var id String
  • iOS 发送 iMessage 尽可能简单

    我希望能够以编程方式发送 iMessage 除了调用一个将文本发送到带有消息的号码的函数之外 无需执行任何其他操作 这两个消息都是文本框 我真的很感激一些示例代码 因为我在网上搜索过 但我发现没有任何帮助 这不适用于商业应用程序 仅适用于我

随机推荐