驱蚊app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

143 line
6.6 KiB

  1. using AppleAuth;
  2. using AppleAuth.Enums;
  3. using AppleAuth.Extensions;
  4. using AppleAuth.Interfaces;
  5. using AppleAuth.Native;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using UnityEngine;
  11. public class UILogin : UIBase
  12. {
  13. private const string AppleUserIdKey = "AppleUserId";
  14. private IAppleAuthManager _appleAuthManager;
  15. public override void OnOpen(params object[] args) {
  16. // If the current platform is supported
  17. if (AppleAuthManager.IsCurrentPlatformSupported)
  18. {
  19. // Creates a default JSON deserializer, to transform JSON Native responses to C# instances
  20. var deserializer = new PayloadDeserializer();
  21. // Creates an Apple Authentication manager with the deserializer
  22. this._appleAuthManager = new AppleAuthManager(deserializer);
  23. }
  24. }
  25. public override void OnClose() { }
  26. private void Update()
  27. {
  28. // Updates the AppleAuthManager instance to execute
  29. // pending callbacks inside Unity's execution loop
  30. if (this._appleAuthManager != null)
  31. {
  32. this._appleAuthManager.Update();
  33. }
  34. }
  35. public void ClickIOSLoginBtn()
  36. {
  37. Debug.Log("IOS Login Clicked");
  38. SignInWithApple();
  39. }
  40. private void SignInWithApple()
  41. {
  42. var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
  43. this._appleAuthManager.LoginWithAppleId(
  44. loginArgs,
  45. credential =>
  46. {
  47. // If a sign in with apple succeeds, we should have obtained the credential with the user id, name, and email, save it
  48. PlayerPrefs.SetString(AppleUserIdKey, credential.User);
  49. this.SetupGameMenu(credential.User, credential);
  50. },
  51. error =>
  52. {
  53. var authorizationErrorCode = error.GetAuthorizationErrorCode();
  54. Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
  55. });
  56. }
  57. private void SetupGameMenu(string appleUserId, ICredential credential)
  58. {
  59. //this.LoginMenu.SetVisible(visible: false);
  60. //this.GameMenu.SetVisible(visible: true);
  61. this.SetupAppleData(appleUserId, credential);
  62. }
  63. public void SetupAppleData(string appleUserId, ICredential receivedCredential)
  64. {
  65. var appleIdCredential = receivedCredential as IAppleIDCredential;
  66. var passwordCredential = receivedCredential as IPasswordCredential;
  67. if (appleIdCredential != null)
  68. {
  69. var stringBuilder = new StringBuilder();
  70. stringBuilder.AppendLine("RECEIVED APPLE ID CREDENTIAL.\nYOU CAN LOGIN/CREATE A USER WITH THIS");
  71. stringBuilder.AppendLine("<b>Username:</b> " + appleIdCredential.User);
  72. stringBuilder.AppendLine("<b>Real user status:</b> " + appleIdCredential.RealUserStatus.ToString());
  73. if (appleIdCredential.State != null)
  74. stringBuilder.AppendLine("<b>State:</b> " + appleIdCredential.State);
  75. if (appleIdCredential.IdentityToken != null)
  76. {
  77. var identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length);
  78. stringBuilder.AppendLine("<b>Identity token (" + appleIdCredential.IdentityToken.Length + " bytes)</b>");
  79. stringBuilder.AppendLine(identityToken.Substring(0, 45) + "...");
  80. }
  81. if (appleIdCredential.AuthorizationCode != null)
  82. {
  83. var authorizationCode = Encoding.UTF8.GetString(appleIdCredential.AuthorizationCode, 0, appleIdCredential.AuthorizationCode.Length);
  84. stringBuilder.AppendLine("<b>Authorization Code (" + appleIdCredential.AuthorizationCode.Length + " bytes)</b>");
  85. stringBuilder.AppendLine(authorizationCode.Substring(0, 45) + "...");
  86. }
  87. if (appleIdCredential.AuthorizedScopes != null)
  88. stringBuilder.AppendLine("<b>Authorized Scopes:</b> " + string.Join(", ", appleIdCredential.AuthorizedScopes));
  89. if (appleIdCredential.Email != null)
  90. {
  91. stringBuilder.AppendLine();
  92. stringBuilder.AppendLine("<b>EMAIL RECEIVED: YOU WILL ONLY SEE THIS ONCE PER SIGN UP. SEND THIS INFORMATION TO YOUR BACKEND!</b>");
  93. stringBuilder.AppendLine("<b>You can test this again by revoking credentials in Settings</b>");
  94. stringBuilder.AppendLine("<b>Email:</b> " + appleIdCredential.Email);
  95. }
  96. if (appleIdCredential.FullName != null)
  97. {
  98. var fullName = appleIdCredential.FullName;
  99. stringBuilder.AppendLine();
  100. stringBuilder.AppendLine("<b>NAME RECEIVED: YOU WILL ONLY SEE THIS ONCE PER SIGN UP. SEND THIS INFORMATION TO YOUR BACKEND!</b>");
  101. stringBuilder.AppendLine("<b>You can test this again by revoking credentials in Settings</b>");
  102. stringBuilder.AppendLine("<b>Name:</b> " + fullName.ToLocalizedString());
  103. stringBuilder.AppendLine("<b>Name (Short):</b> " + fullName.ToLocalizedString(PersonNameFormatterStyle.Short));
  104. stringBuilder.AppendLine("<b>Name (Medium):</b> " + fullName.ToLocalizedString(PersonNameFormatterStyle.Medium));
  105. stringBuilder.AppendLine("<b>Name (Long):</b> " + fullName.ToLocalizedString(PersonNameFormatterStyle.Long));
  106. stringBuilder.AppendLine("<b>Name (Abbreviated):</b> " + fullName.ToLocalizedString(PersonNameFormatterStyle.Abbreviated));
  107. if (appleIdCredential.FullName.PhoneticRepresentation != null)
  108. {
  109. var phoneticName = appleIdCredential.FullName.PhoneticRepresentation;
  110. stringBuilder.AppendLine("<b>Phonetic name:</b> " + phoneticName.ToLocalizedString());
  111. stringBuilder.AppendLine("<b>Phonetic name (Short):</b> " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Short));
  112. stringBuilder.AppendLine("<b>Phonetic name (Medium):</b> " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Medium));
  113. stringBuilder.AppendLine("<b>Phonetic name (Long):</b> " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Long));
  114. stringBuilder.AppendLine("<b>Phonetic name (Abbreviated):</b> " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Abbreviated));
  115. }
  116. }
  117. }
  118. }
  119. public void ClickYkLoginBtn()
  120. {
  121. Debug.Log("YouKe Login Clicked");
  122. }
  123. }