Instance Method

requestAuthorization(options:completionHandler:)

Requests authorization to interact with the user when local and remote notifications are delivered to the user's device.

Declaration

func requestAuthorization(options: UNAuthorizationOptions = [], completionHandler: @escaping (Bool, Error?) -> Void)

Parameters

options

The authorization options your app is requesting. You may combine the available constants to request authorization for multiple items. Request only the authorization options that you plan to use. For a list of possible values, see UNAuthorizationOptions.

completionHandler

The block to execute asynchronously with the results. This block may be executed on a background thread. The block has no return value and has the following parameters:

granted

A Boolean value indicating whether authorization was granted. The value of this parameter is true when authorization was granted for one or more options. The value is false when authorization is denied for all options.

error

An object containing error information or nil if no error occurred.

Discussion

If your app's local or remote notifications involve user interactions, you must request authorization for the system to perfom those interactions on your app's behalf. Interactions include displaying an alert, playing a sound, or badging the app's icon.

The first time your app ever calls the method, the system prompts the user to authorize the requested interactions. The user may grant or deny authorization, and the system stores the user’s response so that subsequent calls to this method do not prompt the user again. After determining the authorization status, the user notification center object executes the block in the completionHandler parameter. Use that block to make any adjustments to your app's behavior. For example, if authorization was denied, you might notify a remote notification server not to send notifications to the user’s device.

The user may change the allowed interactions at any time in system settings. Use the getNotificationSettings(completionHandler:) method to determine what interactions are currently allowed for your app.

See Also

Managing Settings and Authorization

var supportsContentExtensions: Bool

A Boolean value indicating whether the current device supports notification content extensions.

struct UNAuthorizationOptions

Constants for requesting authorization to interact with the user.