Define "Not Defined" Case for Local Notification Settings

Starting with iOS8, you need to register and request a user to use local notifications. Therefore, I would like to implement a way to double check these permissions.

How to check that the Local notifications settings are not defined / not set ? So far, I know how to verify that local notifications are provided or rejected , for example ...

var currentStatus = UIApplication.sharedApplication().currentUserNotificationSettings() var requiredStatus:UIUserNotificationType = UIUserNotificationType.Alert if currentStatus.types == requiredStatus { … //GRANTED } else { … //DENIED } 

The problem with this is that I also got Denied , if nothing is installed yet. How can I differentiate all 3 cases?

  • Provided (notification, warning type)
  • Denied (notification, warning type)
  • Undefined / Not yet installed (therefore, local notification application settings have not yet been created)

As an alternative solution, it would be useful to have a comparable delegate method for CoreLocation authorization didChangeAuthorizationStatus in order to respond to the user's choice in the permission warning. Is there something like this to get user interaction status with privacy notification for local notifications?

+5
source share
2 answers

The solution I implemented:

In the application delegate, I detect when the didRegisterUserNotificationSettings command is removed. And I save the bool value to true in userdefaults:

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

And when I need to know the status:

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

I just found a suitable UIApplication delegate method that helps solve this problem:

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

You will find more information on this at WWDC14 Session 713, β€œWhat's New in iOS Notifications.”

0
source

Source: https://habr.com/ru/post/1205319/


All Articles