Apple
Provide an Apple sign-in option to enable players to authenticate using their Apple accounts on iOS and macOS.
읽는 시간 2분최근 업데이트: 한 달 전
최소 SDK 버전: 2.0.0
이 문서에서는 게임에서 Apple 계정을 사용해 플레이어의 인증을 설정하는 다음 시나리오에 대해 설명합니다.- Apple 로그인 설정
- 기존 사용자 로그인 또는 새 사용자 생성
- 사용자를 익명 로그인에서 Apple 계정을 통한 플랫폼 로그인으로 업데이트
게임에서 플레이어에게 Apple 로그인 옵션을 제공하려면 앱에서 Apple 로그인을 활성화하십시오.
Apple 로그인 설정
- 앱의 Apple 로그인을 활성화합니다. 참고: Unity Authentication은 번들 ID를 통한 로그인만 지원합니다. 서비스 ID는 지원하지 않습니다.
- Unity Authentication의 ID 제공업체를 Apple로 설정합니다.
- Unity 에디터 메뉴에서 Edit > Project Settings… 로 이동한 후, 내비게이션 메뉴에서 Services > Authentication을 선택합니다.
- ID Providers를 Sign-in with Apple로 설정한 후, Add를 선택합니다.
- Bundle ID 텍스트 필드에 Apple 개발자 콘솔의 앱 ID를 입력한 후, Save를 선택합니다. 번들 ID는 com.something.somethingelse와 같은 모습이어야 합니다.
- Unity 프로젝트에 Apple 로그인을 연동하기 위해 Unity 에셋 스토어의 SDK 라이브러리 패키지를 사용할 것을 권장합니다. 한 가지 예로는 Sign in with the Apple Unity Plugin이 있습니다.
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."; } ); }}
기존 플레이어 로그인 또는 신규 플레이어 생성
SignInWithAppleAsync- Apple 자격 증명을 사용해 새 Unity Authentication 플레이어 생성
- Apple 자격 증명을 사용해 기존 플레이어 로그인
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); }}
플레이어를 익명 로그인에서 Apple 계정 로그인으로 업데이트
익명 인증을 설정한 후, 익명 플레이어가 Apple 계정을 생성하고 Apple을 통해 로그인하도록 업그레이드하려는 경우, 게임이 플레이어에게 Apple 로그인 창을 표시하고 Apple에서 ID 토큰을 가져와야 합니다. 그런 다음LinkWithAppleAsync- 를 사용해 캐시된 플레이어의 계정에 로그인합니다.
SignInAnonymouslyAsync - 를 사용해 캐시된 플레이어의 계정을 Apple 계정에 연결합니다.
LinkWithAppleAsync
플레이어가 로그인하거나 새 플레이어 프로필을 생성해 Apple 로그인을 트리거하고 Apple ID 토큰을 가져온 경우, 다음 API를 호출해 플레이어를 인증합니다.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); }}
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 계정 연결을 해제할 수 있도록UnlinkAppleAsyncasync 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); }}