Hi everyone,

In this article, I’ll share how to implement Sign in with Apple in a SwiftUI project. As of now, the Sign in with Apple button and user interface flow is available for implementation only using a view controller in UIKit.

Hence, we’ll create a SwiftUI view for the Sign in with Apple button using UIViewRepresentable, and use the ASAuthorizationController by creating a new class and initialising it in our observable model class.

Note: To implement Sign in with Apple, you’ll need the Apple Developer membership account.

Okay, let’s go step-by-step —

1. Setup your project and developer account

To enable Sign in with Apple in our project, we’ll first need to configure our xcodeproject and register the outbound email domains (optional) on our developer account.

Please visit the official developer documentation and peform the required steps —

Configure Xcode & Configure Private Email Relay Service

Please note, that configuring the private email relay service is an optional step and is only required if your app sends emails to your users.

2. Add Sign in with Apple button

struct AppleSignInButton: UIViewRepresentable {

 func makeUIView(context: Context) -> ASAuthorizationAppleIDButton {
  return ASAuthorizationAppleIDButton()
 }
 func updateUIView(_ uiView: ASAuthorizationAppleIDButton, context:
 Context) {
 }
}

Add the above code in your SwiftUI project, this creates a SwiftUI view for the Sign in with Apple button.

The button can be further configured by using a different initialiser for it, example —

ASAuthorizationAppleIDButton(
    authorizationButtonType: .signUp, 
    authorizationButtonStyle: .white)

The SwiftUI button view can be used simply like this —

var body: some View {
    AppleSignInButton()
}

#combine #programming #swiftui #ios #sign-in-with-apple

Implement Sign in With Apple in a SwiftUI App
8.65 GEEK