티스토리 뷰

유니티(unity)

유니티 ios에서 apple login/logout 구현

개발자 고포고 2024. 1. 3. 23:22
반응형

유니티 ios에서 apple login/logout 구현

 

 

-애플에서 로그아웃 개념이 없음(그냥 뷰로 처리하거나 로컬에 저장-playerprefabs-하는 userID를 제거하여 로그아웃 구현)

 

-최초 로그인을하면 해당 앱(패키지명기준)에 appid.apple.com에 세션이 기록 최초 기록때 이메일, 이름등의 정보를 가져올 수 있지만

세션에 등록한 이후로는 userID,authCode, token정보만 가져올 수 있음, 그렇기때문에 세션에 기록하는걸 회원 가입개념으로 진행하는걸 권장

 

-그래서 애플로그인은 사실 상 인증하는 수단으로 쓰고, 나머지는 그냥 서버나 로컬저장소에서 처리하는 걸 권장한다

 

https://assetstore.unity.com/packages/tools/integration/sign-in-with-apple-unity-plugin-152088

 

Sign in with Apple Unity Plugin | 기능 통합 | Unity Asset Store

Use the Sign in with Apple Unity Plugin from Daniel Lupiañez Casares on your next project. Find this integration tool & more on the Unity Asset Store.

assetstore.unity.com

 

-Sign in with Apple 사용

위 어셋을 이용하여 구현하였다.

불필요한 코드 다 떼어버리고, 최소한의 코드만 사용하였고, 플레이어프리팹으로 로컬데이터 유지도 그냥 떼어버렸다

아래코드를 쓸 줄 알면 그부분은 너무 쉽게 추가 처리 가능하니 최대한 심플하게 진행했다.

 

-샘플코드

using System.Text;
using AppleAuth;
using AppleAuth.Enums;
using AppleAuth.Extensions;
using AppleAuth.Interfaces;
using AppleAuth.Native;
using UnityEngine;
using UnityEngine.Assertions.Must;

public class appleLoginManager : MonoBehaviour
{
    private const string AppleUserIdKey = "AppleUserId";
    
    private IAppleAuthManager _appleAuthManager;

    private void Start()
    {
        // If the current platform is supported
        if (AppleAuthManager.IsCurrentPlatformSupported)
        {
            var deserializer = new PayloadDeserializer();
            this._appleAuthManager = new AppleAuthManager(deserializer);    
        }

        this.InitializeLogin();
    }

    private void Update()
    {
        if (this._appleAuthManager != null)
        {
            this._appleAuthManager.Update();
        }
    }
    
    private void InitializeLogin()
    {
        if (this._appleAuthManager == null)
        {
            Debug.Log("#  지원 안함  #");
            return;
        }
        
        this._appleAuthManager.SetCredentialsRevokedCallback(result =>
        {
            Debug.Log("#  로그인 세션 삭제  #");
            Debug.Log("Received revoked callback " + result);
            // this.SetupLoginMenuForSignInWithApple();
            PlayerPrefs.DeleteKey(AppleUserIdKey);
        });
    }
    
    public void login()
    {
        
        var loginArgs = new AppleAuthLoginArgs(LoginOptions.None);
        Debug.Log("#  로그인 버튼 클릭  #");

        this._appleAuthManager.LoginWithAppleId(
            loginArgs,
            credential =>
            {
                
                Debug.Log("#  로그인 성공  #");
                Debug.Log("# userID: #");
                Debug.Log(credential.User);
                var appleIdCredential = credential as IAppleIDCredential;
                var passwordCredential = credential as IPasswordCredential;
                
                if (appleIdCredential.State != null)
                    Debug.Log("# State: #");
                    Debug.Log( appleIdCredential.State);

                if (appleIdCredential.IdentityToken != null)
                {
                    var identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length);
                    Debug.Log("# identityToken:  #");
                    Debug.Log(identityToken);
                }

                if (appleIdCredential.AuthorizationCode != null)
                {
                    var authorizationCode = Encoding.UTF8.GetString(appleIdCredential.AuthorizationCode, 0, appleIdCredential.AuthorizationCode.Length);
                   Debug.Log("# authorizationCode:  #");
                    Debug.Log(authorizationCode);
                }
                
            },
            error =>
            {
                Debug.Log("#  로그인 실패  #");
                var authorizationErrorCode = error.GetAuthorizationErrorCode();
                Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
            });
    }

    public void LogOut()
    {
        //뷰만 관리
        Debug.Log("#  로그아웃되었습니다  #");
    }
}

 

 

#유니티 #unity #apple #ios #login #logout

반응형
댓글
반응형