using System; using System.Collections; using System.Text; using AppleAuth; using AppleAuth.Enums; using AppleAuth.Extensions; using AppleAuth.Interfaces; using AppleAuth.Native; using Newtonsoft.Json; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; public class AppleLoginHelper : MonoBehaviour { public static AppleLoginHelper ins; //单例记录 private const string AppleUserIdKey = "AppleUserId"; private IAppleAuthManager _appleAuthManager; bool _bSupportedPlatform ; public bool bSupportedPlatform { // 公共属性 get { return this._appleAuthManager == null?false:true; } // getter返回字段的值 set { _bSupportedPlatform = value; } // setter将传入的值赋给字段 } private void Awake() { if (CommonConfig.StandaloneMode) return; string sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name; Debug.Log("AppleLoginHelper当前场景名称: " + sceneName); DontDestroyOnLoad(this); gameObject.name = "AppleLoginHelper"; ins = this; } private void Start() { // 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 this._appleAuthManager = new AppleAuthManager(deserializer); } } private void Update() { // Updates the AppleAuthManager instance to execute // pending callbacks inside Unity's execution loop if (this._appleAuthManager != null) { this._appleAuthManager.Update(); } } //使用苹果正常登录 public void SignInWithAppleButtonPressed() { this.SignInWithApple(); } //删除账号 public void SignInWithAppleButtonPressedDelete(Action success,Action fail) { this.DeleteSignInWithApple(success, fail); } private void SetupLoginMenuForSignInWithApple() { } private void SignInWithApple() { var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName); this._appleAuthManager.LoginWithAppleId( loginArgs, credential => { // If a sign in with apple succeeds, we should have obtained the credential with the user id, name, and email, save it PlayerPrefs.SetString(AppleUserIdKey, credential.User); //this.onAppleData(credential.User, credential); var appleIdCredential = credential as IAppleIDCredential; if (appleIdCredential != null) { //if (appleIdCredential.IdentityToken != null) //{ // var identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length); // Debug.Log("11===identityToken======= " + identityToken); //} //if (appleIdCredential.AuthorizationCode != null) //{ // var authorizationCode = Encoding.UTF8.GetString(appleIdCredential.AuthorizationCode, 0, appleIdCredential.AuthorizationCode.Length); // Debug.Log("22===authorizationCode======= " + authorizationCode.Substring(0, 45)); //} string _email = ""; if (appleIdCredential.Email != null) { _email = appleIdCredential.Email; } Debug.Log("33===Email======= " + _email); string _fullName = ""; if (appleIdCredential.FullName != null) { var fullName = appleIdCredential.FullName; if (appleIdCredential.FullName.PhoneticRepresentation != null) { var phoneticName = appleIdCredential.FullName.PhoneticRepresentation; //phoneticName.ToLocalizedString() } _fullName = fullName.ToLocalizedString(); } Debug.Log("44===FullName======= " + appleIdCredential.FullName); var _identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length); Debug.Log("11===identityToken======= " + _identityToken); LoginView.ins?.OnAppleLoginResp(_identityToken, _email, _fullName); } }, error => { var authorizationErrorCode = error.GetAuthorizationErrorCode(); Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString()); this.SetupLoginMenuForSignInWithApple(); LoginView.ins?.OnAppleLoginError(); }); } private void DeleteSignInWithApple(Action success, Action fail) { var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName); this._appleAuthManager.LoginWithAppleId( loginArgs, credential => { var appleIdCredential = credential as IAppleIDCredential; if (appleIdCredential.AuthorizationCode != null) { var authorizationCode = Encoding.UTF8.GetString(appleIdCredential.AuthorizationCode, 0, appleIdCredential.AuthorizationCode.Length); Debug.Log("delete===authorizationCode======= " + authorizationCode.Substring(0, 45)); StartCoroutine(LoginController.Instance.DdeleteUserByApple(authorizationCode, (res) => { Debug.Log($"DdeleteUserByApple service rescode {res.code}, msg {res.msg}"); if (res.code == 0) { Debug.Log("删除成功"); this.SetupLoginMenuForSignInWithApple(); PlayerPrefs.DeleteKey(AppleUserIdKey); } })); } success(); }, error => { var authorizationErrorCode = error.GetAuthorizationErrorCode(); Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString()); this.SetupLoginMenuForSignInWithApple(); fail(); }); } public bool HasAppleKey() { return PlayerPrefs.HasKey(AppleUserIdKey); } public void DeleteAppleKey() { PlayerPrefs.DeleteKey(AppleUserIdKey); } }