A New Way to Sign in: Sign in with Apple

Batikan Sosun
3 min readApr 20, 2020

--

iOS 13 has come with many innovations. Some of them are quite surprising. We can say that Apple strives to protect user data and at the same time it succeeds with these innovations.

At WWDC 2019, Apple introduces a simple, fast, easy way to sign in to apps. And web pages are also included.

This named as Sign In with Apple help us to set up a user account in our application with name, verified email address(important point), and unique identifiers that allow the user to sign in to your app with their Apple ID.

Once a user sets up their account, they can sign in anywhere you deploy your app.

Let’s start to configure the app

Above all, we need to sets up the capability.

  1. Open the Xcode Project.
  2. Project Navigator→ Select Project → Select Target.
  3. In Project Editor, Click Signing & Capabilities.
  4. Add a Capability by clicking the + button. Search for Sign In with Apple
  5. Double-click the capability to add.

And then the second step for capability; if the auto-signing setting is off you need to add the capability to provisioning profile. Else, it is automatically added to the provisioning profile by Xcode.

One more thing before starting the coding

We need to use Authentication Services(AuthenticationServices.framework) framework to give users the ability to sign into your services with their Apple ID.

In the project main target under the general tab, you will see the Frameworks, Libraries and embedded content. In this section click to plus button and type the library name and then hit the result named AuthenticationServices

Good jobs 👏👏

import the AuthenticationServices frame to the place to be used.

Create Sign In with Apple Button

We will create an instance of ASAuthorizationAppleIDButton and then we will assign a method for preparing for request.

let authorizationButton = ASAuthorizationAppleIDButton()
authorizationButton.addTarget(self, action: #selector(authInWithAppleIDButtonPress), for: .touchUpInside)

Preparing Authorization Request

Body of authInWithAppleIDButtonPress method should be below. ASAuthorizationAppleIDProvider class helps us to prepare the auth request.

let appleIDProvider = ASAuthorizationAppleIDProvider()
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]

let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
authorizationController.performRequests()

Applying Delegate Methods

When authorization is completed this didCompleteWithAuthorization method will be called.

didCompleteWithAuthorization: A delegate that the authorization controller informs about the success or failure of an authorization attempt.

func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
// Create an account in your system.
let userIdentifier = appleIDCredential.user
let userFirstName = appleIDCredential.fullName?.givenName
let userLastName = appleIDCredential.fullName?.familyName
let userEmail = appleIDCredential.email

//Navigate to results view controller
} else if let passwordCredential = authorization.credential as? ASPasswordCredential {
// Sign in using an existing iCloud Keychain credential.
let username = passwordCredential.user
let password = passwordCredential.password

//The app has received your selected credential from the keychain.
}
}

didCompleteWithError: Tells the delegate that authorization failed, and provides an error to explain why.

func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {// Handle error.}

ASAuthorizationControllerPresentationContextProviding this protocol contains a presentationAnchor delegate method. The goal is to tell the delegate from which window it should present content to the user.

func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return self.view.window!
}

Check User Credential State

As soon as the app is opened, maybe you want to know the state of if the user is logged in.

ASAuthorizationAppleIDProvider with this provider, we will know the user state.

let appleIDProvider = ASAuthorizationAppleIDProvider()
appleIDProvider.getCredentialState(forUserID: userIdentifier) { (credentialState, error) in
switch credentialState {
case .authorized:
// The Apple ID credential is valid.
break
case .revoked:
// The Apple ID credential is either revoked or was not found.
break
case .notFound:
// The Apple ID credential is either revoked or was not found.
break
default:
break
}
}

You can place it in the AppDelegate file or such as splash etc.

And the guide is complete. Thank you for reading. I hope that helps you. Please let me know if you have any questions.

--

--

Batikan Sosun
Batikan Sosun

Written by Batikan Sosun

Tweeting tips and tricks about #swift #xcode #apple Twitter @batikansosun Weekly Swift Blogging

Responses (1)