Sign In with Apple: iOS Part (Swift)

Alvaro Orti
Jeff Tech
Published in
3 min readMar 20, 2020

From iOS 13 version, Apple decided to offer a faster, easier and more secure new way of login than those already launched by Facebook and Google.

It offers technical support to any Apple device that needs login.

Should I add login with Apple to my app?

On September 12, 2019 Apple published new version´s guidelines consisted on: any app or update must have the login In implemented with Apple before April 2020.

Although this action requires every app to have login with Apple, there are some advantages.

Advantages

- Apple only uses the username associated to its Apple and it offers you the option of using its identification or a random email that routes the e-mails sent to this address.

- Unlike other login systems, Apple doesn’t share any kind of information to apps.

- It offers you the possibility to use login with Apple in an Android app through Apple JS.

- It offers double factor — 6 digits — authentication to a device or trusted telephone number. Apple devices have a Touch ID or Face ID protection layer so double factor is not offered.

Implementation

  • To create Sign In Apple certificates.
  1. Access to your account https://developer.apple.com/
  2. Go to “Certificates, Identifiers & Profiles — keys” and add one Sign In certificate on every distribution environment you need it
  • To add capability to the project.
  1. Inside project´s root, access to every created environment.
  2. Click on Signing & Capabilities [+ Capability] and select Apple ID Sign In
  • To add button functionality
  1. Import AuthenticationServices library.
  2. Add a button in the screen following design patrons mentioned by Apple. There are two very important design patrons in “Buttons” sections.
  • The button must have an equal or larger size than its competitors.
  • The button must be located in the screen where the user doesn’t need to scroll.

3. In order to design a button look, Apple offers the possibility to add a predefined look over a UIView with ASAuthorizationAppleIDButton directly or the possibility to add a custom content.

4. Apple offers a process of native login. In order to do so, it must be added the delegate of “ASAuthorizationControllerDelegate” where the user´s data will be picked up and it must be added the delegate of “ASAuthorizationControllerPresentationContextProviding” where the instruction of in which screen the process will be showed will be given.

A least it will be necessary to pick up authentication code and code it to .utf8.

  • Validating user

To validate user, Apple need an authentication code. However from February 14, 2020 Apple modified its system by using a previous checking token.

Apple has not yet published the way to validate it, but from the forums.developer.apple.com a thread was created with that incidence.

  1. To run the new way of checking, it will be necessary to take “identityToken” from “ASAuthorizationControllerDelegate”.
  2. Code ID key “kid” kept on the header with “identityToken”. This key will be used to check that token is signed by the Apple private key. In order to give it the allowed format, follow the following steps:
  • To install a manager of external libraries like CocoaPods.
  • To add a JWT codifier like JWTDecoder library.

3. Send the 2 keys to Backend to perform user validation.

Sign in with Apple: Backend Part (Java)

Issues

Although every login with apple id asks if you want to show or hide the email, only what you have selected the first time will be maintained.

To change this setting.

  1. Enter Settings.
  2. Click on the user.
  3. Enter Password & Security.
  4. In case you have selected ‘hide’, delete that setting.
Example with Kayak app

--

--