반응형
유니티용 애플 SDK 설치
코드 작성
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' 카테고리의 다른 글
유니티 플레이팹 아이템 주기 Playfab Grant Item ToUser 간단 사용법 (0) | 2022.07.20 |
---|---|
유니티 플레이팹 아이템 구매 Playfab Shop Purchase Item 간단 사용법 (0) | 2022.07.20 |
유니티 플레이팹 친구 추가, 삭제하기 Playfab Friends Add, Delete 간단 사용법 (0) | 2022.07.19 |
유니티 플레이팹 Json으로 타이틀 데이터 저장, 불러오기 Playfab (0) | 2022.07.19 |
유니티 플레이팹 페이스북 로그인 Playfab Sign ln with Facebook Login 간단 구현 (0) | 2022.07.19 |
유니티 플레이팹 클라우드 스크립트 사용 Playfab CloudScripts 간단 사용법 (0) | 2022.07.12 |
유니티 플레이팹 타이틀 뉴스 가져오기 Playfab ReadTitleNews 간단 사용법 (0) | 2022.07.12 |
유니티 플레이팹 서버 시간 가져오기 Playfab GetServerTime 간단 사용법 (0) | 2022.07.12 |
댓글