Add Sign in with Apple to your app


In this new tutorial, I’ll show you how to add the Sign in with Apple option to your app.

Adding this new button is very simple :

Before starting this tutorail, make sure you’ve added the Sign in with Apple capability

import AuthenticationServices
func setupSiwaView() {
        let siwaButton = ASAuthorizationAppleIDButton()
        siwaButton.translatesAutoresizingMaskIntoConstraints = false
        siwaButton.addTarget(self, action: #selector(didTapSiwaButton), for: .touchUpInside)

        view.addSubview(siwaButton)
    }

Don’t forget to call this function in the viewDidLoad()

 override func viewDidLoad() {
        super.viewDidLoad()
        setupSiwaView()

    }
func didTapSiwaButton() {
        let provider = ASAuthorizationAppleIDProvider()
        let request = provider.createRequest()
        request.requestedScopes = [.fullName, .email]
        
        let controller = ASAuthorizationController(authorizationRequests: [request])
        
        controller.delegate = self
        controller.presentationContextProvider = self
        controller.performRequests()
    }
extension ViewController: ASAuthorizationControllerDelegate {
    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        
        //accepted

    }
    
    func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
        //denied
    }
}
extension ViewController: ASAuthorizationControllerPresentationContextProviding {
    func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
        
        return view.window!

    }
}

After that, you’re done adding Sign in with Apple. You can now get the user full name and email to save his account.

Personalization

You can customize your button in three different ways

let siwaButton = ASAuthorizationAppleIDButton(type: .default, style: .black)

Type

You have three different types: .default, .signUp and .continue
.signUp displays “Sign in with Apple”
.continue displays “Continue with Apple”

Style

You also have three differents styles: .black, .white and .whiteOutline. See the guidelines for more informations.

Corner radius

siwaButton.cornerRadius = 10

You can customize the corner radius of the button from square to the maximum corner radius.

If you liked this tutorial, share it with your friends or should you have any feedback, contact me

All images: Apple