_AuthenticationServices_SwiftUI · IOS API REFERENCE
AuthorizationController
struct · iOS 16.4+ · SDK 27.1
@MainActor struct AuthorizationController
A value provided in the SwiftUI environment that views can use
to perform authorization requests.
Use the ``SwiftUI/Environment`` property wrapper to get an instance
of this structure from the ``EnvironmentValues/authorizationController``
environment value.
For example, you can perform authorization requests when the
user taps a button:
struct AuthorizationControllerExample: View {
@Environment(\.authorizationController) private var authorizationController
var body: some View {
Button("Sign In") {
Task {
do {
async let requests = authorizationRequests() // defined elsewhere
let result = try await authorizationController
.performRequests(requests)
switch result {
// code to handle the authorization result
}
} catch {
// code to handle the authorization error
}
}
}
}
}
Perform the authorization request with additional options.
For example, you can use ``SwiftUI/View/task(priority:_:)`` or
``SwiftUI/View/task(id:priority:_:)`` along with
``AuthenticationServices/ASAuthorizationController/RequestOptions/preferImmediatelyAvailableCredentials``
to perform an authorization request when the view appears:
.task {
async let request = authorizationRequest() // defined elsewhere
let result = try await authorizationController
.performRequest(
request,
options: .preferImmediatelyAvailableCredentials)
switch result {
// code to handle the authorization result
}
}
- Parameters:
- request: The authorization request to perform.
- options: Additional options to customize the behavior of the
authorization controller.
Perform the authorization request.
For example, you can perform an authorization request when the
user taps a button:
struct AuthorizationControllerExample: View {
@Environment(\.authorizationController) private var authorizationController
var body: some View {
Button("Sign In") {
Task {
do {
async let request = authorizationRequest() // defined elsewhere
let result = try await authorizationController
.performRequest(request)
switch result {
// code to handle the authorization result
}
} catch {
// code to handle the authorization error
}
}
}
}
}
- Parameter request: The authorization request to perform.
Perform the authorization requests with additional options.
For example, you can use ``SwiftUI/View/task(priority:_:)`` or
``SwiftUI/View/task(id:priority:_:)`` along with
``AuthenticationServices/ASAuthorizationController/RequestOptions/preferImmediatelyAvailableCredentials``
to perform authorization requests when the view appears:
.task {
async let requests = authorizationRequests() // defined elsewhere
let result = try await authorizationController
.performRequests(
requests,
options: .preferImmediatelyAvailableCredentials)
switch result {
// code to handle the authorization result
}
}
- Parameters:
- requests: The authorization requests to perform.
- options: Additional options to customize the behavior of the
authorization controller.
Perform the authorization requests.
For example, you can perform authorization requests when the
user taps a button:
struct AuthorizationControllerExample: View {
@Environment(\.authorizationController) private var authorizationController
var body: some View {
Button("Sign In") {
Task {
do {
async let requests = authorizationRequests() // defined elsewhere
let result = try await authorizationController
.performRequests(requests)
switch result {
// code to handle the authorization result
}
} catch {
// code to handle the authorization error
}
}
}
}
}
- Parameter requests: The authorization requests to perform.
Perform the AutoFill-assisted authorization request.
Add the ``UIKit/UITextContentType/username`` text content type to the
account or login name field:
TextField("User name", text: $username)
.textContentType(.username)
Use ``SwiftUI/View/task(priority:_:)`` or ``SwiftUI/View/task(id:priority:_:)``
to perform an AutoFill-assisted authorization request when the view appears:
.task {
async let request = autoFillAuthorizationRequest() // defined elsewhere
let result = try await authorizationController
.performAutoFillAssistedRequest(request)
switch result {
// code to handle the authorization result
}
}
- Parameter requests: The AutoFill-assisted authorization
requests to perform.
Perform the AutoFill-assisted authorization requests.
Add the ``UIKit/UITextContentType/username`` text content type to the
account or login name field:
TextField("User name", text: $username)
.textContentType(.username)
Use ``SwiftUI/View/task(priority:_:)`` or ``SwiftUI/View/task(id:priority:_:)``
to perform AutoFill-assisted authorization requests when the view appears:
.task {
async let requests = autoFillAuthorizationRequests() // defined elsewhere
let result = try await authorizationController
.performAutoFillAssistedRequests(requests)
switch result {
// code to handle the authorization result
}
}
- Parameter requests: The AutoFill-assisted authorization
requests to perform.