iOS
...
Parse Swift SDK
Users

Sign In with Apple

12min

Sign In with Apple

Introduction

When integrating third-party sign-in methods on an iOS app, it is mandatory to add the sign in with Apple option as an additional alternative. For this purpose, Apple introduced the AuthenticationServices framework. This framework allows developers to seamlessly integrate a Sign in with Apple button on any Xcode project.

In this repository we provide a simple Xcode template where you can test the different sign in methods we are implementing. This example was already introduced in the log in guide. You can revisit it for more details about the project.

Prerequisites

To complete this quickstart, you need:

  • A recent version of Xcode.
  • An Apple developer account with non-personal developer team.
  • An app created at Back4App.

Goal

To integrate a user sign-in feature using the AuthenticationServices framework and ParseSwift SDK.

1 - Setting up Sign in with Apple

  • Once we have the Xcode project linked to the Back4App application, we proceed to add the Sign in with Apple capability. To do this, select your project from the project navigator and go to the targets section. Select the main target and go to the Signing & Capabilities tab, then click on the + Capability button and add the Sign in with Apple capability:
Document image


This is the only configuration needed to start integrating a Sign in with Apple method on an iOS app.

2 - Using the AuthenticationServices framework with ParseSwift

The sign in with Apple flow can be completed in three stages. But before, let us add and set up the button to be used to present the Sign in with Apple flow. In the LogInControllerclass we add this button:

Swift


Note that the LogInController conforms to two new protocols: ASAuthorizationControllerDelegate and ASAuthorizationControllerPresentationContextProviding. The first protocol allows us to delegate the sign-in result to the LogInController class. The second protocol is to determine the UIWindow where the sign-in sheet is displayed.

We now implement the flow in the handleSignInWithApple() method.

  • In the first stage, we prepare the request and the form. The request is constructed by the ASAuthorizationAppleIDRequest class. We get an instance of this class from the ASAuthorizationAppleIDProvider provider and the form by the ASAuthorizationController class. Once we have an instance of the request, we have to provide the scopes we are interested in. So far Apple only gives access to the user’s full name and email. Thus, a standard way to create a request is:
Swift

  • With this request, we construct an ASAuthorizationController controller. This controller is in charge of displaying a sheet where the user authenticates and gives the corresponding permissions to complete the sign-in process:
Swift

  • In the last stage we handle the sign-in result. This is returned via the delegate method authorizationController(controller:didCompleteWithAuthorization:). The last argument in this method is an ASAuthorization class containing all the necessary information about the Apple ID and credentials. This stage is where we associate a User object and perform the log in to the Back4App application. This User object has the following structure (see the Login guide for more details):
Swift


Now, we create a User object from the data contained in the ASAuthorization result. We accomplish this by instantiating a ParseApple object (from User.apple) and call the login(user:identityToken:) method:

Swift


3 - Verifying user sign in and session creation

To make sure that the Google sign-in worked, you can look at your Back4App application dashboard and see the new User containing the Facebook authData parameters.

Document image


You can also verify that a valid session was created in the dashboard, containing a pointer to the corresponding User object.

Document image


4 - Linking an existing User to an Apple ID

In case your iOS App requires relating an Apple ID to an existing user in your Back4App platform, the ParseApple<User> object implements the method link(user:identityToken:completion:) where you pass the user value and the identityToken from a ASAuthorizationAppleIDCredential credential

Swift


5 - Run the app

You can go to this repository and download the example project. Before running the project, make sure you set up the provisioning profiles with the ones associated with your developer account.

Conclusion

At the end of this guide, you learned how to sign in or link existing Back4App users on iOS using the sign in with Apple.