Apple

최소 SDK 버전: 2.0.0

이 문서에서는 게임에서 Apple 계정을 사용해 플레이어의 인증을 설정하는 다음 시나리오에 대해 설명합니다.

  • Apple 로그인 설정
  • 기존 사용자 로그인 또는 새 사용자 생성
  • 사용자를 익명 로그인에서 Apple 계정을 통한 플랫폼 로그인으로 업데이트

중요: Unity Authentication SDK에서 지원하는 Apple 로그인은 iOS 플랫폼의 ID 토큰을 가진 단일 번들 ID에만 작동합니다. Android 플랫폼의 서비스 ID와 인증 코드를 사용하는 플레이어를 위한 Apple 로그인은 지원하지 않습니다.

게임에서 플레이어에게 Apple 로그인 옵션을 제공하려면 앱에서 Apple 로그인을 활성화하십시오.

참고: 아래 코드 예시에서는 플레이어의 Apple ID 토큰을 이미 획득했다고 가정합니다.

Apple 로그인 설정

  1. 앱의 Apple 로그인을 활성화합니다. 참고: Unity Authentication은 번들 ID를 통한 로그인만 지원합니다. 서비스 ID는 지원하지 않습니다.
  2. Unity Authentication의 ID 제공업체를 Apple로 설정합니다.
    1. Unity 에디터 메뉴에서 Edit > **Project Settings…**로 이동한 후, 내비게이션 메뉴에서 Services > Authentication을 선택합니다.
    2. ID ProvidersSign-in with Apple로 설정한 후, Add를 선택합니다.
    3. Bundle ID 텍스트 필드에 Apple 개발자 콘솔의 앱 ID를 입력한 후, Save를 선택합니다. 번들 ID는 com.something.somethingelse와 같은 모습이어야 합니다.
  3. Unity 프로젝트에 Apple 로그인을 연동하기 위해 Unity 에셋 스토어의 SDK 라이브러리 패키지를 사용할 것을 권장합니다. 한 가지 예로는 Sign in with the Apple Unity Plugin이 있습니다.
    1. 설치하려면 패키지 기술 자료의 단계를 따르십시오.
    2. 참고: 참조된 패키지는 Unity가 개발하거나 보유, 운영하지 않습니다. Unity가 제공하지 않는 패키지의 사용법은 베스트 프랙티스를 참고하십시오.
    3. 아래 코드 스니핏에는 이 예시 패키지를 사용해 Unity 프로젝트에서 Apple 로그인을 활성화하는 방법이 나와 있습니다.
using System.Text;
using UnityEngine;

// External dependencies
using 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.";
            }
        );
    }
}

기존 플레이어 로그인 또는 신규 플레이어 생성

SignInWithAppleAsync 메서드를 사용하여 다음 중 하나를 수행할 수 있습니다.

  • Apple 자격 증명을 사용해 새 Unity Authentication 플레이어 생성
  • Apple 자격 증명을 사용해 기존 플레이어 로그인

프로젝트에서 자격 증명과 연결된 Unity Authentication 플레이어가 존재하지 않는 경우, SignInWithAppleAsync가 새 플레이어를 생성합니다. 프로젝트에서 자격 증명과 연결된 Unity Authentication 플레이어가 존재하는 경우, SignInWithAppleAsync가 해당 플레이어의 계정으로 로그인합니다. 이 기능은 캐시된 플레이어를 고려하지 않으며, SignInWithAppleAsync가 캐시된 플레이어를 대체합니다.

async 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);
    }
}

플레이어를 익명 로그인에서 Apple 계정 로그인으로 업데이트

익명 인증을 설정한 후, 익명 플레이어가 Apple 계정을 생성하고 Apple을 통해 로그인하도록 업그레이드하려는 경우, 게임이 플레이어에게 Apple 로그인 창을 표시하고 Apple에서 ID 토큰을 가져와야 합니다. 그런 다음 LinkWithAppleAsync API를 호출해 플레이어를 Apple ID 토큰에 연결합니다.

SDK에 캐시된 플레이어가 존재하는 경우, 캐시된 플레이어를 Apple 계정에 연결할 수 있습니다.

  1. SignInAnonymouslyAsync를 사용해 캐시된 플레이어의 계정에 로그인합니다.
  2. LinkWithAppleAsync를 사용해 캐시된 플레이어의 계정을 Apple 계정에 연결합니다.

캐시된 플레이어에 대한 자세한 내용은 캐시된 사용자 로그인 섹션을 참고하십시오.

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);
    }
}

플레이어가 로그인하거나 새 플레이어 프로필을 생성해 Apple 로그인을 트리거하고 Apple ID 토큰을 가져온 경우, 다음 API를 호출해 플레이어를 인증합니다.

async 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);
    }
}

Apple 로그인 인증 구현

플레이어가 로그인하거나 새 플레이어 프로필을 생성해 Apple 로그인을 트리거하고 Apple ID 토큰을 가져온 경우, 다음 API를 호출해 플레이어를 인증합니다. SignInWithAppleAsync(string idToken).

Apple 계정 연결 해제

플레이어가 Apple 계정 연결을 해제할 수 있도록 UnlinkAppleAsync API를 사용합니다. 연결이 해제되면 계정이 다른 ID에 연결되지 않은 경우 익명 계정으로 전환됩니다.

async Task UnlinkAppleAsync(string idToken)
{
   try
   {
       await AuthenticationService.Instance.UnlinkAppleAsync(idToken);
       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);
   }
}