驱蚊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.
 
 
 
 
 

212 lines
8.6 KiB

  1. using AppleAuth;
  2. using AppleAuth.Enums;
  3. using AppleAuth.Extensions;
  4. using AppleAuth.Interfaces;
  5. using AppleAuth.Native;
  6. using UnityEngine;
  7. namespace AppleAuthSample
  8. {
  9. public class MainMenu : MonoBehaviour
  10. {
  11. private const string AppleUserIdKey = "AppleUserId";
  12. private IAppleAuthManager _appleAuthManager;
  13. public LoginMenuHandler LoginMenu;
  14. public GameMenuHandler GameMenu;
  15. private void Start()
  16. {
  17. // If the current platform is supported
  18. if (AppleAuthManager.IsCurrentPlatformSupported)
  19. {
  20. // Creates a default JSON deserializer, to transform JSON Native responses to C# instances
  21. var deserializer = new PayloadDeserializer();
  22. // Creates an Apple Authentication manager with the deserializer
  23. this._appleAuthManager = new AppleAuthManager(deserializer);
  24. }
  25. this.InitializeLoginMenu();
  26. }
  27. private void Update()
  28. {
  29. // Updates the AppleAuthManager instance to execute
  30. // pending callbacks inside Unity's execution loop
  31. if (this._appleAuthManager != null)
  32. {
  33. this._appleAuthManager.Update();
  34. }
  35. this.LoginMenu.UpdateLoadingMessage(Time.deltaTime);
  36. }
  37. public void SignInWithAppleButtonPressed()
  38. {
  39. this.SetupLoginMenuForAppleSignIn();
  40. this.SignInWithApple();
  41. }
  42. private void InitializeLoginMenu()
  43. {
  44. this.LoginMenu.SetVisible(visible: true);
  45. this.GameMenu.SetVisible(visible: false);
  46. // Check if the current platform supports Sign In With Apple
  47. if (this._appleAuthManager == null)
  48. {
  49. this.SetupLoginMenuForUnsupportedPlatform();
  50. return;
  51. }
  52. // If at any point we receive a credentials revoked notification, we delete the stored User ID, and go back to login
  53. this._appleAuthManager.SetCredentialsRevokedCallback(result =>
  54. {
  55. Debug.Log("Received revoked callback " + result);
  56. this.SetupLoginMenuForSignInWithApple();
  57. PlayerPrefs.DeleteKey(AppleUserIdKey);
  58. });
  59. // If we have an Apple User Id available, get the credential status for it
  60. if (PlayerPrefs.HasKey(AppleUserIdKey))
  61. {
  62. var storedAppleUserId = PlayerPrefs.GetString(AppleUserIdKey);
  63. this.SetupLoginMenuForCheckingCredentials();
  64. this.CheckCredentialStatusForUserId(storedAppleUserId);
  65. }
  66. // If we do not have an stored Apple User Id, attempt a quick login
  67. else
  68. {
  69. this.SetupLoginMenuForQuickLoginAttempt();
  70. this.AttemptQuickLogin();
  71. }
  72. }
  73. private void SetupLoginMenuForUnsupportedPlatform()
  74. {
  75. this.LoginMenu.SetVisible(visible: true);
  76. this.GameMenu.SetVisible(visible: false);
  77. this.LoginMenu.SetSignInWithAppleButton(visible: false, enabled: false);
  78. this.LoginMenu.SetLoadingMessage(visible: true, message: "Unsupported platform");
  79. }
  80. private void SetupLoginMenuForSignInWithApple()
  81. {
  82. this.LoginMenu.SetVisible(visible: true);
  83. this.GameMenu.SetVisible(visible: false);
  84. this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: true);
  85. this.LoginMenu.SetLoadingMessage(visible: false, message: string.Empty);
  86. }
  87. private void SetupLoginMenuForCheckingCredentials()
  88. {
  89. this.LoginMenu.SetVisible(visible: true);
  90. this.GameMenu.SetVisible(visible: false);
  91. this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
  92. this.LoginMenu.SetLoadingMessage(visible: true, message: "Checking Apple Credentials");
  93. }
  94. private void SetupLoginMenuForQuickLoginAttempt()
  95. {
  96. this.LoginMenu.SetVisible(visible: true);
  97. this.GameMenu.SetVisible(visible: false);
  98. this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
  99. this.LoginMenu.SetLoadingMessage(visible: true, message: "Attempting Quick Login");
  100. }
  101. private void SetupLoginMenuForAppleSignIn()
  102. {
  103. this.LoginMenu.SetVisible(visible: true);
  104. this.GameMenu.SetVisible(visible: false);
  105. this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
  106. this.LoginMenu.SetLoadingMessage(visible: true, message: "Signing In with Apple");
  107. }
  108. private void SetupGameMenu(string appleUserId, ICredential credential)
  109. {
  110. this.LoginMenu.SetVisible(visible: false);
  111. this.GameMenu.SetVisible(visible: true);
  112. this.GameMenu.SetupAppleData(appleUserId, credential);
  113. }
  114. private void CheckCredentialStatusForUserId(string appleUserId)
  115. {
  116. // If there is an apple ID available, we should check the credential state
  117. this._appleAuthManager.GetCredentialState(
  118. appleUserId,
  119. state =>
  120. {
  121. switch (state)
  122. {
  123. // If it's authorized, login with that user id
  124. case CredentialState.Authorized:
  125. this.SetupGameMenu(appleUserId, null);
  126. return;
  127. // If it was revoked, or not found, we need a new sign in with apple attempt
  128. // Discard previous apple user id
  129. case CredentialState.Revoked:
  130. case CredentialState.NotFound:
  131. this.SetupLoginMenuForSignInWithApple();
  132. PlayerPrefs.DeleteKey(AppleUserIdKey);
  133. return;
  134. }
  135. },
  136. error =>
  137. {
  138. var authorizationErrorCode = error.GetAuthorizationErrorCode();
  139. Debug.LogWarning("Error while trying to get credential state " + authorizationErrorCode.ToString() + " " + error.ToString());
  140. this.SetupLoginMenuForSignInWithApple();
  141. });
  142. }
  143. private void AttemptQuickLogin()
  144. {
  145. var quickLoginArgs = new AppleAuthQuickLoginArgs();
  146. // Quick login should succeed if the credential was authorized before and not revoked
  147. this._appleAuthManager.QuickLogin(
  148. quickLoginArgs,
  149. credential =>
  150. {
  151. // If it's an Apple credential, save the user ID, for later logins
  152. var appleIdCredential = credential as IAppleIDCredential;
  153. if (appleIdCredential != null)
  154. {
  155. PlayerPrefs.SetString(AppleUserIdKey, credential.User);
  156. }
  157. this.SetupGameMenu(credential.User, credential);
  158. },
  159. error =>
  160. {
  161. // If Quick Login fails, we should show the normal sign in with apple menu, to allow for a normal Sign In with apple
  162. var authorizationErrorCode = error.GetAuthorizationErrorCode();
  163. Debug.LogWarning("Quick Login Failed " + authorizationErrorCode.ToString() + " " + error.ToString());
  164. this.SetupLoginMenuForSignInWithApple();
  165. });
  166. }
  167. private void SignInWithApple()
  168. {
  169. var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
  170. this._appleAuthManager.LoginWithAppleId(
  171. loginArgs,
  172. credential =>
  173. {
  174. // If a sign in with apple succeeds, we should have obtained the credential with the user id, name, and email, save it
  175. PlayerPrefs.SetString(AppleUserIdKey, credential.User);
  176. this.SetupGameMenu(credential.User, credential);
  177. },
  178. error =>
  179. {
  180. var authorizationErrorCode = error.GetAuthorizationErrorCode();
  181. Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
  182. this.SetupLoginMenuForSignInWithApple();
  183. });
  184. }
  185. }
  186. }