Sign in with Apple in SwiftUI

How to implement Sign in with Apple in SwiftUI?

Answered by BabyJ in 626184022
This is the new easiest way with SwiftUI.

Code Block Swift
SignInWithAppleButton(.signIn) { request in
request.reqestedScopes = [.fullName, .email]
} onCompletion: { result in
switch result {
case .success(let authResults):
print("Authorisation successful")
case .error(let error):
print("Authorisation failed: \(error.localizedDescription)")
}
}
// black button
.signInWithAppleButtonStyle(.black)
// white button
.signInWithAppleButtonStyle(.white)
// white with border
.signInWithAppleButtonStyle(.whiteOutline)


Or see more in the documentation for this.
Accepted Answer
This is the new easiest way with SwiftUI.

Code Block Swift
SignInWithAppleButton(.signIn) { request in
request.reqestedScopes = [.fullName, .email]
} onCompletion: { result in
switch result {
case .success(let authResults):
print("Authorisation successful")
case .error(let error):
print("Authorisation failed: \(error.localizedDescription)")
}
}
// black button
.signInWithAppleButtonStyle(.black)
// white button
.signInWithAppleButtonStyle(.white)
// white with border
.signInWithAppleButtonStyle(.whiteOutline)


Or see more in the documentation for this.
The documentation link provided in the accepted answer cannot be found. Can anyone point me to more information about configuring this?
Apple seems to have just changed the page address. This is the new link.

This approach is excellent. However, I got identityToken and authorizationCode, but I can't get the full user name or email. Is there some additional reason with this method why I can't get them?

Fast forward to 2022

import SwiftUI
import AuthenticationServices

struct AuthView: View {
  var body: some View {
    SignInWithAppleButton(.continue) { request in
      request.requestedScopes = [.fullName, .email]
    } onCompletion: { result in
      switch result {
        case .success(let authResults):
          print("Authorisation successful")
        case .failure(let error):
          print("Authorisation failed: \(error.localizedDescription)")
      }
    }
    // black button
    .signInWithAppleButtonStyle(.black)
    // white button
    .signInWithAppleButtonStyle(.white)
    // white with border
    .signInWithAppleButtonStyle(.whiteOutline)
  }
}
Sign in with Apple in SwiftUI
 
 
Q