Sign in with Apple in SwiftUI

How to implement Sign in with Apple in SwiftUI?

Accepted Reply

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.
  • Nice one

  • Great answer - may I just point out a few additional details needed for this to work:

    import AuthenticationServicesrequest.requestedScopes = [.fullName, .email] (typo : you missed the 'u')case .failure instead of case.error

    Please update your answer if you can.

Add a Comment

Replies

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.
  • Nice one

  • Great answer - may I just point out a few additional details needed for this to work:

    import AuthenticationServicesrequest.requestedScopes = [.fullName, .email] (typo : you missed the 'u')case .failure instead of case.error

    Please update your answer if you can.

Add a Comment
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?

  • @matejaOpacic Can you please explain how did you get the identityToken and authorization Code ?

Add a Comment

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)
  }
}
Add a Comment