Преглед на файлове

Get email and full name

alex_gs преди 3 години
родител
ревизия
bdd214d12c
променени са 1 файла, в които са добавени 83 реда и са изтрити 16 реда
  1. 83 16
      Unity3/Assets/AppleAuthSample/MainMenu.cs

+ 83 - 16
Unity3/Assets/AppleAuthSample/MainMenu.cs

@@ -188,23 +188,90 @@ public class MainMenu : MonoBehaviour
     
     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);
-                Debug.Log(credential.User);
-                //this.SetupGameMenu(credential.User, credential);
-            },
-            error =>
-            {
-                var authorizationErrorCode = error.GetAuthorizationErrorCode();
-                Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
-                this.SetupLoginMenuForSignInWithApple();
-            });
-        
+        //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);
+        //        Debug.Log(credential.User);
+        //        //this.SetupGameMenu(credential.User, credential);
+        //    },
+        //    error =>
+        //    {
+        //        var authorizationErrorCode = error.GetAuthorizationErrorCode();
+        //        Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
+        //        this.SetupLoginMenuForSignInWithApple();
+        //    });
+    this._appleAuthManager.LoginWithAppleId(
+    loginArgs,
+    credential =>
+    {
+        // Obtained credential, cast it to IAppleIDCredential
+        var appleIdCredential = credential as IAppleIDCredential;
+        if (appleIdCredential != null)
+        {
+            // Apple User ID
+            // You should save the user ID somewhere in the device
+            var userId = appleIdCredential.User;
+            PlayerPrefs.SetString(AppleUserIdKey, userId);
+
+            // Email (Received ONLY in the first login)
+            var email = appleIdCredential.Email;
+
+            // Full name (Received ONLY in the first login)
+            var fullName = appleIdCredential.FullName;
+
+            // Identity token
+            var identityToken = System.Text.Encoding.UTF8.GetString(
+                appleIdCredential.IdentityToken,
+                0,
+                appleIdCredential.IdentityToken.Length);
+
+            // Authorization code
+            var authorizationCode = System.Text.Encoding.UTF8.GetString(
+                appleIdCredential.AuthorizationCode,
+                0,
+                appleIdCredential.AuthorizationCode.Length);
+            Debug.Log(userId + " " + fullName + " " + email);
+
+            var _name = fullName.GivenName;
+            var _surname = fullName.MiddleName;
+
+            // Print out information about the user who logged in.
+            SocialManager.Instance.userID = userId;
+            SocialManager.Instance._name = _name;
+            SocialManager.Instance._surname = _surname;
+            SocialManager.Instance._usermail = email;
+            SocialManager.Instance._username = userId;
+            SocialManager.Instance._deviceId = GameDatabaseManager.Instance.GetDevice();
+            PlayerPrefs.SetString("UserID", userId);
+            PlayerPrefs.SetInt("Login", 1);
+            PlayerPrefs.Save();
+            GameDatabaseManager.Instance.RegisterUser(userId, _name, _surname, _name + " " + _surname, email, GameDatabaseManager.Instance.GetDevice(), OnLoginCompleted);
+            Debug.Log(
+                string.Format("Display Name: {0}\nEmail: {1}\nUser ID: {2}\nID Token: {3}", _name + " " + _surname ?? "",
+                    email ?? "", userId ??  ""));
+
+
+            //   
+            // And now you have all the information to create/login a user in your system
+        }
+    },
+    error =>
+    {
+        // Something went wrong
+        var authorizationErrorCode = error.GetAuthorizationErrorCode();
+    });
+
+    }
+    private void OnLoginCompleted(bool arg1, string arg2)
+    {
+        Debug.Log("arg2 " + arg2);
+        PlayerPrefs.SetInt("APPLE", 1);
+        PlayerPrefs.Save();
     }
 }