How to add ‘Sign in with Apple’ button to your iOS app using Swift
- October 21, 2019
- John Codeos
Apple announced back in June, at WWDC 2019, their own social login service called ‘Sign in with Apple‘ with a focus on privacy.
Users can hide their own email address by giving a private email, created by Apple, to the app.
Apple added recently in their App Store Review Guidelines that the apps who have already 3rd party social logins (Facebook Login, Sign in with Twitter, e.t.c) to set up an account, must also add the ‘Sign in with Apple‘ method.
Today I’m going to show you how to integrate the ‘Sign in with Apple‘ button into your iOS project in 4 simple steps.
Let’s dive into it!
Step 1: Set up your app in the Apple Developer Account
Go into your Apple Developer Account, choose Certificates, Identifiers & Profiles, then choose Identifiers on the left, and then your app from the list.
On the list Capabilities, enable Sign In with Apple, press Edit, choose Enable as a primary App ID and press Save.
Press Save again and Confirm.
Now, go back and click the Keys button on the left.
Create a new key by pressing the + button next to the ‘Keys’ title.
Give a name and enable Sign In with Apple by ticking the box.
Press Configure and choose your Primary App ID and press Save.
Press Continue, in the next page press Register, and then Done (You don’t need to download the key, because we’re not going to use ‘Sign in with Apple‘ in a website)
(Optional) Add Domain and e-mail to contact your users
As I said at the beginning, ‘Sign in with Apple‘ gives the users the option to hide their email address when they login.
To be able to contact those users, you need to set up a domain and a contact email in your Apple Developer Account.
Choose More from left and press Configure.
In the next page, add your domain and email and press Register on both
To verify the domain, press Download and download the .txt file.
Go into your website’s FTP server, create a folder with a name .well-known (yes, with the dot in front) and paste it inside.
To test if it’s working, type: https://PUT_YOUR_DOMAIN_HERE.com/.well-known/apple-developer-domain-association.txt and check if a text comes out as a result.
After that, go back and press Verify. (It might take time until Apple ‘see’ the file on your server.)
If it’s all good, you’ll see the following page:
Step 2: Add ‘Sign in with Apple’ capability in your project
Go into your project and choose your project from the left side, press Signing & Capabilities, and then press + Capability to add a new capability.
On the new window double click Sign in with Apple to add it (Type ‘apple’ to find it easier)
Step 3: Add ‘Sign in with Apple’ Button
There’s two ways you can add the ‘Sign in with Apple’ button in your view.
You can use the class ASAuthorizationAppleIDButton, provided by Apple, or you can create your own custom button (Style the button according to Apple’s Sign in with Apple – Human Interface Guidelines to prevent your app from getting rejected on the review process).
Option 1: Adding ASAuthorizationAppleIDButton Button
Add the following code in your LoginViewController:
Option 2: Adding Custom Button
Add the following code in your LoginViewController if you want to have a custom button instead of using the ASAuthorizationAppleIDButton class:
Step 4: Getting Users Data
At the top of your swift file, add the AuthenticationServices framework.
Add the ASAuthorizationControllerDelegate delegate to your class to manage the authorization results.
Also add the ASAuthorizationControllerPresentationContextProviding to set the window we want to present the authorization process.
Add the following function in your LoginViewController class:
In this function, we set the scopes, in our example, we want the user’s full name and email.
If the authentication failed, we print the error, if succeeded, we print user’s id, first name, last name, and email.
If the user uses iCloud keychain, returns the username and password.
Important: When the user re-login it returns only the user’s ID. Apple suggests storing the user’s ID with their data(first name, last name, email) and retrieving them when they re-login.
Check the user’s credentials state
Check the user’s credentials by using the getCredentialState method from the ASAuthorizationAppleIDCredential
In this example we check if the user have removed or revoked our app from the list who gives access to their Apple ID, and if they did, then we show the ‘Sign in’ screen, else we continue to show the ‘User’s Profile’ screen
Remove Existing Account from your Apple ID
If you want to test again the ‘Sign in with Apple‘ registration, you can do it by removing your app from the list in Settings > Press on your Apple ID > Password & Security > Apps Using Your Apple ID
You can find the final project here
If you have any questions feel free to DM me on Twitter @johncodeos or leave a comment below!