Ios 识别;“未确定”;本地通知设置的案例

Ios 识别;“未确定”;本地通知设置的案例,ios,swift,uilocalnotification,privacy,Ios,Swift,Uilocalnotification,Privacy,从iOS8开始,您需要注册并提示用户使用本地通知。因此,我想实现一种双重检查这些权限的方法 如何检查本地通知设置未确定/未设置的情况?到目前为止,我只知道如何检查本地通知是否被授予或拒绝,如下所示 var currentStatus = UIApplication.sharedApplication().currentUserNotificationSettings() var requiredStatus:UIUserNotificationType = UIUserNotif

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

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

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

    if currentStatus.types == requiredStatus {
     … //GRANTED
     } else {
     … //DENIED
     }
使用它的问题是,如果到目前为止没有设置任何设置,我也会得到一个拒绝的。如何区分所有3种情况

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

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

我刚刚找到了一个合适的UIApplication委托方法,可以帮助解决此问题:

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

您可以在WWDC14会话713“iOS通知中的新增功能”中找到有关此功能的更多详细信息。

我实施的解决方案:

在应用程序委托中,我检测何时触发didRegisterUserNotificationSettings。我将bool保存在userdefaults中为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
}