Adding Sign In with Apple Option into your SwiftUI App

Jan 4, 2023

ZoZo App allows you to share photos, llnks or files & get access to ZoZoGPT right from your keyboard

One of the biggest trends in iOS apps is allowing your Users to sign in with their Apple ID. Having this option helps improve the UX of your app since Users don’t have to go through the time to put in their information to create an account. 

Now they can sign in with their Apple ID which contains all the information they would add when signing up manually. Using this option also helps protect the Users privacy since they will have the option to share their Apple ID’s email or not.

Since ZoZo App (this is our blog if you didn’t notice!) is built on SwiftUI we thought it would be a great idea to show you how to add Sign In with Apple into your iOS App. 

Fun fact, ZoZo doesn’t require Users to sign in or make an account. We made it so that all the iOS Keyboard Shortcuts you create for the content you want to share at any time are stored on your device. Our team has ZER access to your information & the Keyboard Shortcuts you create. The only thing we track are how many times you use a Keyboard Shortcut! 

Step 1 - Open Xcode 

First thing you’re going to want to do is open Xcode and create a SwiftUI App. 

Step 2 - Create Apple Sign In Button

Add the following code to add the Sign In with Apple button into your app. In the next step we will make the server request to actually sign the User in. 

import SwiftUI

import AuthenticationServices

struct SignInWithAppleButton: UIViewRepresentable {

    func makeUIView(context: Context) -> ASAuthorizationAppleIDButton {

        return ASAuthorizationAppleIDButton()

    }


    func updateUIView(_ uiView: ASAuthorizationAppleIDButton, context: Context) {}

}


struct ContentView: View {

    var body: some View {

        SignInWithAppleButton()

            .frame(width: 280, height: 44)

            .onTapGesture {

                // Perform sign in with Apple here

            }

    }

}

Step 3 - Server Request

Add the following code to your app to allow Users to Sign In with Apple. This code sends a request to Apples Servers after the User signs with Apple.

ASAuthorizationAppleIDButton.onAuthorizationCompleted = { authorization, error in

    if let authorization = authorization {

        let credential = ASAuthorizationAppleIDCredential(authorization: authorization)

        let authorizationCode = credential.authorizationCode

        let identityToken = credential.identityToken

        let user = credential.user

        let email = credential.email


        // Send the authorization code, identity token, and user information to your server

        // Your server should then use these to create a session token


        // Once you have a session token, you can use it to authenticate the user in your app

        // For example, you can store it in the user defaults or in the keychain

    } else if let error = error {

        // Handle the error

    }

}

This code snippet assumes that you have imported the AuthenticationServices framework and have configured an ASAuthorizationAppleIDButton in your app.

When the user taps the button and successfully authenticates with their Apple ID, the onAuthorizationCompleted event is triggered and the completion block is executed. The authorization parameter contains an ASAuthorization object, which you can use to obtain the authorization code and other information about the user.

You can then send this information to your server, which should use it to create a session token. The session token can be used to authenticate the user in your app. You can store it in the user defaults or in the keychain, for example.

Conclusion

Adding Sign In with Apple is crucial to increasing your Apps User Experience & there are rumors that Apple is requiring every app in the App Store to have this option. The code we added will allow people to Sign In with Apple but also allow you to customize where the token containing the Users information is stored. 

Enjoyed this article? Share it with your developer friends!