This video Add Sign in with Apple to Your iOS App was last updated on Nov 15 2022
In this video we’re going to add Sign in with Apple to your iOS app and learn how to show the button on the login screen. Please note that you’ll need an Apple developer account to test this out.
Adding the capability
Open the TILiOS project in Xcode and navigate to the TILiOS target. Change the bundle identifier to something unique to your project. For example, this video will use dev.timc.siwa-demo.TILiOS
.
Next click on the Signing & Capabilities tab and select your development team to ensure the app is signed correctly.
Displaying the button
Now that you can do so, it’s time to show the Sign in with Apple button on the log in screen. Open LoginView.swift and at the top of the file, import AuthenticationServices
:
import AuthenticationServices
SignInWithAppleButton(.signIn) { request in
} onCompletion: { result in
}
request.requestedScopes = [.fullName, .email]
@MainActor
func handleSIWA(result: Result<ASAuthorization, Error>) async throws -> Token {
}
switch result {
case .failure(let error):
self.showingLoginErrorAlert = true
print("Error \(error)")
throw error
}
case .success(let authResult):
if let credential = authResult.credential as? ASAuthorizationAppleIDCredential {
} else {
self.showingLoginErrorAlert = true
throw AuthError.badResponse
}
guard
let identityToken = credential.identityToken,
let tokenString = String(data: identityToken, encoding: .utf8)
else {
print("Failed to get token from credential")
self.showingLoginErrorAlert = true
throw AuthError.badResponse
}
let name: String?
if let nameProvided = credential.fullName {
name = "\(nameProvided.givenName ?? "") \(nameProvided.familyName ?? "")"
} else {
name = nil
}
throw AuthError.notLoggedIn
Task {
let apiToken = try await handleSIWA(result: result)
}
auth.token = apiToken.value