ios - 识别本地通知设置的 "Not Determined"案例

标签 ios swift uilocalnotification privacy

从 iOS8 开始,您需要注册并提示用户使用本地通知。因此,我想实现一种方法来仔细检查这些权限。

如何检查本地通知设置是否未确定/未设置?到目前为止,我只知道如何检查本地通知是否被granteddenied,像这样......

    var currentStatus = UIApplication.sharedApplication().currentUserNotificationSettings()
    var requiredStatus:UIUserNotificationType = UIUserNotificationType.Alert

    if currentStatus.types == requiredStatus {
     … //GRANTED
     } else {
     … //DENIED
     }

使用这个的问题是,如果到目前为止没有任何设置,我也会得到一个Denied。如何区分所有 3 种情况?

  • 已授予(通知、警报类型)
  • 拒绝(通知、警报类型)
  • 未定义/尚未设置(因此尚未创建本地通知应用设置)

作为替代解决方案,如果有一个与 CoreLocation 的授权 didChangeAuthorizationStatus 类似的委托(delegate)方法,以便对用户在权限警报上的选择使用react,将会很有帮助。是否有类似的东西来获取用户与本地通知的隐私警报交互的状态?

最佳答案

我实现的解决方案:

在应用委托(delegate)中,我检测到 didRegisterUserNotificationSettings 何时被触发。我在 userdefaults 中保存一个 bool 为 true :

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "notificationsDeterminedKey")
        NSUserDefaults.standardUserDefaults().synchronize()
}

当我需要知道状态时:

if NSUserDefaults.standardUserDefaults().boolForKey("notificationsDeterminedKey") {
    let grantedSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
    if grantedSettings.types == UIUserNotificationType.None {
        // Denied
    } else {
        // Granted
} else {
    // Not Determined
}

关于ios - 识别本地通知设置的 "Not Determined"案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26518827/

相关文章:

ios - 在单元格中保存事件指示器的状态

iphone - 设置 UILocalNotification 的 fireDate

swift - 以编程方式更改导航标题

ios新游戏项目,矩形显示在屏幕外?

iphone - 删除并重新安装 iphone 应用程序后接收本地通知

iphone - 仅在特定日期触发UILocalNotification

ios - 自定义 UICollectionViewCell 不显示 UIImage 和崩溃

ios - 从 C# 到 Objective C 的 Http 请求

ios - UITabBar 宽度不随屏幕尺寸增加

ios - 带有 URL 的 NSAttributedString 不起作用