【问题标题】:Identify "Not Determined" case for Local Notifications settings确定本地通知设置的“未确定”情况
【发布时间】:2014-12-18 13:47:06
【问题描述】:

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

如何检查本地通知设置未确定/未设置的情况?到目前为止,我只知道如何检查本地通知是否被授予拒绝,像这样......

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

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

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

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

作为一种替代解决方案,有一个与 CoreLocation 的授权 didChangeAuthorizationStatus 类似的委托方法会很有帮助,以便对用户在权限警报上的选择做出反应。为了获取用户与本地通知的隐私警报的交互状态,是否有类似的东西?

【问题讨论】:

    标签: ios swift uilocalnotification privacy


    【解决方案1】:

    我实施的解决方案:

    在应用程序委托中,我检测到 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
    }
    

    【讨论】:

      【解决方案2】:

      我刚刚找到了一个合适的 UIApplication 委托方法来帮助解决这个问题:

      - (void)application:(UIApplication *)application
          didRegisterUserNotificationSettings:
          (UIUserNotificationSettings *)notificationSettings {
      

      您可以在 WWDC14 Session 713“iOS 通知中的新功能”中找到更多详细信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-06
        • 1970-01-01
        • 1970-01-01
        • 2013-08-25
        相关资源
        最近更新 更多