Apple
Provide an Apple sign-in option to enable players to authenticate using their Apple accounts on iOS and macOS.
Read time 3 minutesLast updated 15 hours ago
Minimum SDK version: 2.0.0
This article guides you through the following scenarios in setting up authentication for players in your game with an Apple account:- Set up an Apple sign-in.
- Sign in a returning user or create new user.
- Update a user from an anonymous sign-in to a platform sign-in via an Apple account.
To provide an Apple sign-in option for the players in your game, set up your app to enable sign-in with Apple.
Set up an Apple sign-in
- Set up your app to enable sign-in with Apple. Note: Unity Authentication only supports Bundle ID based sign-in. Service ID is not supported.
- Configure your ID provider to be Apple for Unity Authentication:
- In the Unity Editor menu, go to Edit > Project Settings…, then select Services > Authentication from the navigation menu.
- Set ID Providers to Sign-in with Apple, then select Add.
- Enter the Bundle ID from the Apple developer console, in the App ID text field, then select Save. The Bundle ID should look like this: com.something.somethingelse.
- To integrate sign-in with Apple within your Unity project, we recommend leveraging an SDK library package from the Unity Asset Store. One example is Sign in with the Apple Unity Plugin.
- To install, follow the steps in the documentation of the package.
- Note: The referenced package is not developed, owned, or operated by Unity.
- The below code snippet shows how you can leverage this example package within your Unity project to enable Apple sign-in.
using System.Text;using UnityEngine;// External dependenciesusing AppleAuth;using AppleAuth.Enums;using AppleAuth.Interfaces;using AppleAuth.Native;public class AppleExampleScript : MonoBehaviour{ IAppleAuthManager m_AppleAuthManager; public string Token { get; private set; } public string Error { get; private set; } public void Initialize() { var deserializer = new PayloadDeserializer(); m_AppleAuthManager = new AppleAuthManager(deserializer); } public void Update() { if (m_AppleAuthManager != null) { m_AppleAuthManager.Update(); } } public void LoginToApple() { // Initialize the Apple Auth Manager if (m_AppleAuthManager == null) { Initialize(); } // Set the login arguments var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName); // Perform the login m_AppleAuthManager.LoginWithAppleId( loginArgs, credential => { var appleIDCredential = credential as IAppleIDCredential; if (appleIDCredential != null) { var idToken = Encoding.UTF8.GetString( appleIDCredential.IdentityToken, 0, appleIDCredential.IdentityToken.Length); Debug.Log("Sign-in with Apple successfully done. IDToken: " + idToken); Token = idToken; } else { Debug.Log("Sign-in with Apple error. Message: appleIDCredential is null"); Error = "Retrieving Apple Id Token failed."; } }, error => { Debug.Log("Sign-in with Apple error. Message: " + error); Error = "Retrieving Apple Id Token failed."; } ); }}
Sign in a returning player or create new player
You can use theSignInWithAppleAsync- Create a new Unity Authentication player with the Apple credentials.
- Sign in an existing player using the Apple credentials.
SignInWithAppleAsyncSignInWithAppleAsyncSignInWithAppleAsyncasync Task SignInWithAppleAsync(string idToken){ try { await AuthenticationService.Instance.SignInWithAppleAsync(idToken); Debug.Log("SignIn is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
Update a player from anonymous to an Apple account
After you’ve set up anonymous authentication, if an anonymous player wants to upgrade to creating an Apple account, then sign in using Apple, your game should prompt the player to trigger the Apple sign-in and get the ID token from Apple. Then, call theLinkWithAppleAsync- Sign into the cached player's account using .
SignInAnonymouslyAsync - Link the cached player's account to the Apple account with .
LinkWithAppleAsync
async Task LinkWithAppleAsync(string idToken){ try { await AuthenticationService.Instance.LinkWithAppleAsync(idToken); Debug.Log("Link is successful."); } catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked) { // Prompt the player with an error message. Debug.LogError("This user is already linked with another account. Log in instead."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
Unlink Apple account
Use theUnlinkAppleAsyncasync Task UnlinkAppleAsync(){ try { await AuthenticationService.Instance.UnlinkAppleAsync(); Debug.Log("Unlink is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}