Adopting Sign In With Apple
Create a sign in button, verify data and handle user authorization changes
With Apple’s recent announcement at WWDC of Sign In With Apple, you as a developer can give your users a whole new option to create an account and sign into your application. Let’s dive in and take a look at how to build Sign In With Apple into your app.
Enable Sign In With Apple to Your App Bundle ID
Log in to your developer account. Under your app identifier setting -> Capabilities, check Sign in with Apple.
Enable Sign In With Apple From Xcode
Open the Xcode 11 beta or a newer version. In project settings, select target -> Signing & Capabilities, add Sign in with Apple.
- Create a “Sign in with Apple” button.
- Ask the user for authorization.
- Verify the user data against your stored data (locally or from the server).
- Handle the user authorization change.
1. Create a Sign In With Apple Button
2. Ask the User for Authorization
handleAuthorization, it asks for the user authorization with a
- When creating
AsAuthorizationController, you need to provide both
delegateprovide authorization request states — failed (for an error) or success.
presentationContextProviderasks for a window to show the authorization UI.
3. Verify the User Data Against Your Stored Data (Locally or From the Server).
At step 2, you will get a user
if let credential = authorization.credential as? ASAuthorizationAppleIDCredential
Some information from
credential is worth mentioning:
- User email:
- User name:
- User identifier:
- Authorization state:
- Refresh token:
let code = credential.authorizationCode, let codeStr = String(data: code, encoding: .utf8)
- Access token:
let idToken = credential.identityToken, let tokeStr = String(data: idToken, encoding: .utf8)
4. Handling the User Authorization Change
Users can always change their authorization for an app. It’s our job to know about the change early enough, and handle the change.
In order to handle the change, we need to store the
information received from step 3.
AppDelegate, we just pass the
user identifier to
ASAuthorizationAppleIDProvider to verify, and you may receive the following states:
Please note that users can remove an app’s authorization from the iOS settings page at any time, so we need to check it regularly.