IOS
[IOS]유니티 애플로 로그인 구현하기 2
파울로 디발자
2023. 1. 13. 14:58
[실제 구현]
애플 로그인 구현하기 1 포스트에서는 git hub의 프로젝트를 살펴보았다. 이를 실제 프로젝트에 적용해보았다.공부하는 용도이기에 틀리거나 빠진것 말씀 해주시면 감사하겠습니다 ..!
[로그인]
만약 처음 로그인이라면 애플 로그인을 할 것이고 , 이미 한 상태라면 해당 애플 계정이 유효한지 확인한다.
public void Login(System.Action<bool, string> _callback)
{
//최초 1회 로그인을 했다면
if (PlayerPrefs.HasKey(AppleUserIdKey))
{
var storedAppleUserId = PlayerPrefs.GetString(AppleUserIdKey);
this.CheckCredentialStatusForUserId(storedAppleUserId,_callback);
}
else
{
var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
this._appleAuthManager.LoginWithAppleId(
loginArgs,
credential =>
{
PlayerPrefs.SetString(AppleUserIdKey, credential.User);
_callback(true, "I" + credential.User.Replace(".", "")); //성공시 액션
},
error =>
{
_callback(false, ""); //실패시 액션
});
}
}
유효한지 확인하는 메서드
private void CheckCredentialStatusForUserId(string appleUserId, System.Action<bool, string> _callback)
{
// If there is an apple ID available, we should check the credential state
this._appleAuthManager.GetCredentialState(
appleUserId,
state =>
{
switch (state)
{
// If it's authorized, login with that user id
case CredentialState.Authorized:
_callback(true, "I" + appleUserId.Replace(".", ""));
return;
// If it was revoked, or not found, we need a new sign in with apple attempt
// Discard previous apple user id
case CredentialState.Revoked:
case CredentialState.NotFound:
_callback(false, "");
PlayerPrefs.DeleteKey(AppleUserIdKey);
return;
}
},
error =>
{
var authorizationErrorCode = error.GetAuthorizationErrorCode();
Debug.LogWarning("Error while trying to get credential state " + authorizationErrorCode.ToString() + " " + error.ToString());
_callback(false, "");
if (PlayerPrefs.HasKey(AppleUserIdKey))
{
PlayerPrefs.DeleteKey(AppleUserIdKey);
}
});
}