iOS 13 — Sign In with Apple Tutorial

Implementing “Sign In with Apple” easily in your app.

What is Sign In with Apple?

“Sign In with Apple” is a new authentication feature in iOS 13. It helps both developers and users to sign up and go into apps with an easy, simple and secure way.

Do I have to add it?

If you are using some third-party login methods like Google or Facebook, then the answer is yes — you have to use it, otherwise, your app may be rejected and won’t pass Apple review process.

Adding Sign In With Apple Capability

Under the Xcode project file, there is Signing & Capabilities available. Press on the + and add the “Sign In with Apple” capability. Adding the capability will create appropriate entitlements file and the right framework.

Adding capability

Adding the Button

The build in “Sign in with Apple” button

First we need to import a framework called “AuthenticationServices”

AuthenticationServices was added to the SDK last year in iOS 12 and is used to handle passwords saved in the keychain. Now it’s also responsible of the Sign In with Apple Feature.

import AuthenticationServices

After that , adding the button is very simple — Just create a button from class ASAuthorizationAppleIDButton. ASAuthorizationAppleIDButton actually inherited from UIControl and not UIButton but it doesn’t really matter since all we need to do is adding a target and an action to it and that’s all, like in all UIControl’s. All the other button issues such as highlighting, titles, etc are already being take care for you under the hood

There are no official ways to customize this button, but you can change it’s dimensions.

After we add the button, a target and an action, we need to response to user tap.

Creating the button

Open the Dialog

The response for the button tap contains 7 steps:

  1. Create an instance of ASAuthorizationAppleIDProvider. This provider is responsible for generating requests for authentication based on AppleID.
  2. From this provider, call “createRequest ()” method.
  3. Define what scopes you want to receive from the user. Currently, only Email and Full name are available.
  4. Define the controller delegate, so you can respond to the user actions in the dialog.
  5. Provide a presentationContextProvider, so the controller can tell from what window to open.
  6. Create an AuthorizationController from those requests.
  7. Call performRequests method.
Responding to the button tap

This will open a dialog for the user, but with some issues, you need to be aware of:

  1. The user can edit his first and last name before sharing it with the app.
  2. If the user has several email addresses associated to his account, he can choose each one of them.
  3. The user can choose to hide his real email from the app and Apple will generate a random email for him. Now, the user can’t see the random email Apple generated for him and the app can’t see the real email for the user. Also, Apple generates the same email next time; the user will try to sign up/into your app. This is extremely important since a lot of apps are base their users account on email addresses.
The pop iOS SDK for sign in with your Apple ID

Response to user Authorization

The delegate we set for the controller has 2 methods: One is success (didCompleteWithAuthorization) and other is error.

Let’s go over the didCompleteWithAuthorization”.
So besides the controller itself, this method has an additional parameter called “authorization” from type “ASAuthorization”. This object contains all the required data from the authorization process

Extract email and useriD

We need the extract the credential from the authorization object and there we have it — the user data.

What’s included here?

First of all, we have the basic data — Full Name and Email, and just to remind you, it doesn’t have to be the real name and email of the user.

On the other hand, we also have some more info which you may find useful. For example, we have a user identifier. You should save it in your app keychain.

Handling Changes in Apple ID State

We know that the user can sign out from his Apple ID account or not letting the app permission to use it. Fortunately, we have an explicit notification for that, which is called NSNotification.Name.ASAuthorizationAppleIDProviderCredentialRevoked.
You need to listen to this notification and then get the credential state for the user ID you got when the user logged in by using ASAuthorizationAppleIDProvider.

Responding to state changes

Existing Accounts

If your user signed using email and password before and the password is attached to his keychain, then it will also offer the user to sign in using his existing email address and password.

In order to handle the response, just check the authorization.credential in the didCompleteWithAuthorization delegate method and in the case from type ASPasswordCredential, you know that the user signed in with a saved email and password.

Sending both password and Apple ID requests
Define between password credential and appleID credential

Creating a Generic Solution for Sign In with the Third- Party Provider

Now, that we have Google, Facebook, and Apple ID login. You should think of creating a robust and unified solution for signing and logging to your app.
One of the methods of doing that, is to create a connector for each one of your login methods, which conform to some “authentication connector protocol”. So all you will have to do is to call that connector with some connect () protocol method and received success or failure with an email and a name.

That will make your work much simpler.

Web/Android Solution

This is a tricky part — if currently, your app is using some third-party authentication service, such as Google or Facebook; you will have to implement Sign In with Apple method. This is Apple guidelines and if your app is cross-platform, for example, if you have a web version of your app, it means that you probably will have to implement Sign In with Apple on your web as well.

Remember, the user can share a fake email with you and he will have to do it also on the web.

Apple releases a JavaScript SDK especially for that, letting you build your own controls for web and for Android platforms, so you can make a cross-platform solution of the new authentication method.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s