Search code examples
iosswiftoptimizationnotificationsuilocalnotification

Determining whether a call to `registerUserNotificationSettings` has ever been made in Swift


I’m wanting to determine if a user has ever received an alert requesting to registerUserNotificationSettings UIUserNotificationSettings.

As an example, when accessing Photos, it’s possible to determine if there has been a request to access Photos and permission granted PHPhotoLibrary.authorizationStatus(). It returns the status as .Authorized, .Denied, .Restricted, .NotDetermined. When there has been no requests to date, .NotDetermined is returned.

Similarly, when registering notifications and getting permission to do so from the user for the first time UIApplication.sharedApplication().registerUserNotificationSettings, it is possible to determine types of notifications registered, .Badge, .Alert, .Sound, .None. (Note: .None can mean both there has been no request or that there has been a request, but the user has elected for no notifications.)

Question:

But is there a way to determine whether the registerUserNotificationSettings has ever been called before? i.e. is there an equivalent to Photos .NotDetermined?


Solution

  • When you call registerUserNotificationSettings, the UIApplicationDelegate method:

    func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings)
    

    will be called letting you know the result. Use this method to save relevant state information so the next time the app runs, you know whether it has been called before or not.