requestAuthorization(options:completionHandler:)
Requests a person’s authorization to allow local and remote notifications for your app.
Declaration
func requestAuthorization(options: UNAuthorizationOptions = [], completionHandler: @escaping @Sendable (Bool, (any Error)?) -> Void)func requestAuthorization(options: UNAuthorizationOptions = []) async throws -> BoolParameters
- 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 execute on a background thread. The block has no return value and has the following parameters:
- granted
A Boolean value indicating whether the person grants authorization. The value of this parameter is True when the person grants authorization for one or more options. The value is False when the person denies authorization or authorization is undetermined. Use Getnotificationsettings(completionhandler:) to check the authorization status.
- error
An object containing error information or
nilif no error occurs.
Mentioned in
Discussion
If your app’s local or remote notifications involve user interactions, you must request authorization for the system to perform 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 calls the method, the system prompts the person to authorize the requested interactions. The person may grant or deny authorization, and the system stores the person’s response. Subsequent calls to this method don’t prompt the person 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 the person denied authorization, you might notify a remote notification server not to send notifications to the user’s device.
The person may change the interactions they allow at any time in system settings. Use the getNotificationSettings(completionHandler:) method to determine what interactions are allowed for your app.
let center = UNUserNotificationCenter.current()
do {
if try await center.requestAuthorization(options: [.badge, .sound, .alert]) == true {
// You have authorization.
} else {
// You don't have authorization.
}
} catch {
// Handle any errors.
}