본문 바로가기
개발/Playfab

유니티 플레이팹 애플 로그인 Playfab Sign ln with Apple Login 간단 구현

by SPNK 2022. 7. 19.
반응형

유니티용 애플 SDK 설치

 

Release Sign in with Apple Unity Plugin v1.4.2 · lupidan/apple-signin-unity

Changed Handles empty NSPersonNameComponents sent by Apple when not requesting a name, to be nil natively. Updated MacOSAppleAuthManager.bundle with the updated native code Removed Removes FixSe...

github.com

 

코드 작성

using System.Collections;
using System.Text;
using PlayFab;
using PlayFab.ClientModels;
#if UNITY_IOS
using AppleAuth;
using AppleAuth.Native;
using AppleAuth.Enums;
using AppleAuth.Interfaces;
using AppleAuth.Extensions;
#endif
using UnityEngine;

public class PlayfabManager : MonoBehaviour
{
#if UNITY_IOS
    private const string AppleUserIdKey = "AppleUserId";
    private IAppleAuthManager _appleAuthManager;
#endif

    private void DisplayPlayfabError(PlayFabError error) => Debug.LogError("error : " + error.GenerateErrorReport());

    void Awake()
    {
#if UNITY_IOS
        IOSActivate();
#endif
    }


#if UNITY_IOS
    private void IOSActivate() //애플 초기화
    {
        // If the current platform is supported
        if (AppleAuthManager.IsCurrentPlatformSupported)
        {
            // Creates a default JSON deserializer, to transform JSON Native responses to C# instances
            var deserializer = new PayloadDeserializer();
            // Creates an Apple Authentication manager with the deserializer
            _appleAuthManager = new AppleAuthManager(deserializer);
        }
        StartCoroutine(AppleAuthUpdate());
    }
    
    IEnumerator AppleAuthUpdate() //초기화 코루틴
    {
        while (true)
        {
            _appleAuthManager?.Update();
            yield return null;
        }
    }
#endif


#if UNITY_IOS
    public void OnClickAppleLogin() //애플 로그인
    {
        Debug.Log("Try Apple Login");
        StartCoroutine(AppleLoginCor());
    }

    void SignInWithApple()
    {
        var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);

        _appleAuthManager.LoginWithAppleId(
            loginArgs,
            credential =>
            {
                var appleIdCredential = credential as IAppleIDCredential;
                if (appleIdCredential != null)
                {
                    OnClickAppleLogin(appleIdCredential.IdentityToken);
                }
            }, error =>
            {
                var authorizationErrorCode = error.GetAuthorizationErrorCode();
            });
    }
    
    
    IEnumerator AppleLoginCor() //애플 로그인 코루틴
    {
        var _newAppleUser = false;
        IOSActivate();
        while (_appleAuthManager == null) yield return null;
        if(!_newAppleUser)
        {var quickLoginArgs = new AppleAuthQuickLoginArgs();

        _appleAuthManager.QuickLogin(
            quickLoginArgs,
            credential =>
            {
                var appleIdCredential = credential as IAppleIDCredential;
                if (appleIdCredential != null)
                {
                    OnClickAppleLogin(appleIdCredential.IdentityToken);
                }
            },
            error =>
            {
                _newAppleUser=true;
                SignInWithApple();
                var authorizationErrorCode = error.GetAuthorizationErrorCode();
            });
        }
        else
        {
            SignInWithApple();
        }
        yield return null;
    }
    

    void OnClickAppleLogin(byte[] identityToken)
    {
        PlayFabClientAPI.LoginWithApple(new LoginWithAppleRequest
        {
            CreateAccount = true,
            IdentityToken = Encoding.UTF8.GetString(identityToken),
            TitleId = PlayFabSettings.TitleId
        }
        , result =>
        {
            Debug.Log("Apple Login Success");
            OnLoginSuccess(result);
        }
        , DisplayPlayfabError);
    }
    
    
    public void OnClickAppleLink(bool forceLink = false) //게스트 -> 애플 로그인 링크
    {
        var quickLoginArgs = new AppleAuthQuickLoginArgs();

        _appleAuthManager.QuickLogin(quickLoginArgs, credential =>
        {
            var appleIdCredential = credential as IAppleIDCredential;
            if (appleIdCredential != null)
            {
                TryLinkAppleAccount(appleIdCredential.IdentityToken, forceLink);
            }
        }, error =>
        {
            var authorizationErrorCode = error.GetAuthorizationErrorCode();
        });
    }
    
    
    void TryLinkAppleAccount(byte[] identityToken, bool forceLink)
    {
        PlayFabClientAPI.LinkApple(new LinkAppleRequest
        {
            ForceLink = forceLink,
            IdentityToken = Encoding.UTF8.GetString(identityToken)
        }
        , result =>
        {
            Debug.Log("Link Apple Success!!");
        }
        , DisplayPlayfabError);
    }

#endif

    public void OnLoginSuccess(PlayFab.ClientModels.LoginResult result) //성공
    {
        Debug.Log("Playfab Apple Login Success");
    } 
}

 


참고할만한 글

 

플레이팹 Playfab 게스트 로그인 Guest Login 간단 사용법

using PlayFab; using PlayFab.ClientModels; using PlayFab.Json; using PlayFab.ProfilesModels; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using Entity..

parksh3641.tistory.com

 

유니티 플레이팹 구글 로그인 Playfab Sign In with Google Login 간단 구현

구글 로그인 SDK 설치 https://github.com/playgameservices/play-games-plugin-for-unity/tree/master/current-build GitHub - playgameservices/play-games-plugin-for-unity: Google Play Games plugin for Unity Google Play Games plugin for Unity. Contribute t

parksh3641.tistory.com

 

반응형

댓글