Skip to content

Sign in with Apple

Stanislav Osipov edited this page Mar 15, 2021 · 4 revisions

As you may already know, if you are using third-party or social login service (such as Facebook Login, Google Sign-In, Sign in with Twitter, Sign In with LinkedIn, Login with Amazon, or WeChat Login) you will be also required to add Apple Sing in into your application.

AppStore review guidelines:

https://developer.apple.com/app-store/review/guidelines/#sign-in-with-apple

4.8 Sign in with Apple

Apps that exclusively use a third-party or social login service (such as Facebook Login, Google Sign-In, Sign in with Twitter, Sign In with LinkedIn, Login with Amazon, or WeChat Login) to set up or authenticate the user’s primary account with the app must also offer Sign in with Apple as an equivalent option. A user’s primary account is the account they establish with your app for the purposes of identifying themselves, signing in, and accessing your features and associated services.

Sign in with Apple is not required if:

  • Your app exclusively uses your company’s own account setup and sign-in systems.

  • Your app is an education, enterprise, or business app that requires the user to sign in with an existing education or enterprise account.

  • Your app uses a government or industry-backed citizen identification system or electronic ID to authenticate users.

  • Your app is a client for a specific third-party service and users are required to sign in to their mail, social media, or other third-party account directly to access their content.

So let's talk how you can add Apple Sing in into your Game / Project:

Enable AuthenticationServices

Make sure that AuthenticationServices is enabled in the plugin settings before you start implementation:

SigninwithApple

Request Authorization with Apple ID

Code snippet how to start sign-in flow below. You can begin sing-in as soon as the user clicks on the sing in button in your app.

using SA.iOS.UIKit;
using SA.iOS.AuthenticationServices;
...

var provider = new ISN_ASAuthorizationAppleIDProvider();
var request = provider.CreateRequest();

var requests = new ISN_ASAuthorizationRequest[] { request };
var authorizationController = new ISN_ASAuthorizationController(requests);

var @delegate = new AuthorizationDelegateExample();
authorizationController.SetDelegate(@delegate);
authorizationController.PerformRequests();

The result delivered into the delegate you set to ISN_ASAuthorizationController. Here is AuthorizationDelegateExample class implementation that was used as a delegate instance in the previous snippet.

using SA.Foundation.Templates;
using SA.iOS.AuthenticationServices;
using UnityEngine;

public class AuthorizationDelegateExample : ISN_IASAuthorizationControllerDelegate
{
    public void DidCompleteWithAuthorization(ISN_ASAuthorizationAppleIDCredential credential)
    {
        Debug.LogError("Apple Sing In Completed: ");
        Debug.Log("credential.User: " + credential.User);
        Debug.Log("credential.Email: " + credential.Email);
        Debug.Log("credential.State: " + credential.State);

        Debug.Log("credential.AuthorizationCode: " + credential.AuthorizationCode);
        Debug.Log("credential.IdentityToken: " + credential.IdentityToken);

        Debug.Log("credential.FullName.Nickname: " + credential.FullName.Nickname);
        Debug.Log("credential.FullName.FamilyName: " + credential.FullName.FamilyName);
        Debug.Log("credential.FullName.GivenName: " + credential.FullName.GivenName);
        Debug.Log("credential.FullName.MiddleName: " + credential.FullName.MiddleName);
        Debug.Log("credential.FullName.NamePrefix: " + credential.FullName.NamePrefix);
        Debug.Log("credential.FullName.NameSuffix: " + credential.FullName.NameSuffix);
    }

    public void DidCompleteWithError(SA_Error error)
    {
        Debug.LogError("Apple Sing In Failed: " + error.FullMessage);
    }
}

Request Existing Credentials

The code snippet below checks if the user has an existing account by requesting both an Apple ID and an iCloud keychain password.

var requests = new ISN_ASAuthorizationRequest[]
{
    new ISN_ASAuthorizationAppleIDProvider().CreateRequest(),
    new ISN_ASAuthorizationPasswordProvider().CreateRequest()
};

var authorizationController = new ISN_ASAuthorizationController(requests);
var @delegate = new AuthorizationDelegateExample();
authorizationController.SetDelegate(@delegate);
authorizationController.PerformRequests();

Check User Credentials at Launch

Only show the Sign in with the Apple user interface when necessary. Checks the status of the saved user credentials immediately after launch.

The GetCredentialStateForUserID function retrieves the state of the user identifier saved in the keychain. If the user granted authorization for the app (for example, the user is signed into the app with their Apple ID on the device).

var appleIDProvider = new ISN_ASAuthorizationAppleIDProvider();
appleIDProvider.GetCredentialStateForUserID(currentUserIdentifier,
    (credentialState, error) =>
    {
        if (error != null)
        {
            Debug.LogError(error.FullMessage);
            return;
        }

        switch (credentialState)
        {
            case ISN_ASAuthorizationAppleIDProviderCredentialState.Authorized:
                // The Apple ID credential is valid.
                break;
            
            case ISN_ASAuthorizationAppleIDProviderCredentialState.Revoked:
            case ISN_ASAuthorizationAppleIDProviderCredentialState.NotFound:
                // The Apple ID credential is either revoked or was not found,
                // so show the sign-in UI.
                break;
        }
        
    });

About

Foundation

AV Foundation

App Tracking Transparency

Game Kit

Store Kit

UI Kit

Social

Replay Kit

Contacts

AVKit

Photos

App Delegate

User Notifications

MediaPlayer

Core Location

AdSupport

EventKit

CloudKit

Authentication Services

XCode

Knowledge Base

Clone this wiki locally