驱蚊app
您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
 
 
 
 
 

91 行
4.0 KiB

  1. #if UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS
  2. using System;
  3. using System.Reflection;
  4. using UnityEditor.iOS.Xcode;
  5. namespace AppleAuth.Editor
  6. {
  7. public static class ProjectCapabilityManagerExtension
  8. {
  9. /// <summary>
  10. /// Extension method for ProjectCapabilityManager to add the Sign In With Apple capability in compatibility mode.
  11. /// In particular, adds the AuthenticationServices.framework as an Optional framework, preventing crashes in
  12. /// iOS versions previous to 13.0
  13. /// </summary>
  14. /// <param name="manager">The manager for the main target to use when adding the Sign In With Apple capability.</param>
  15. public static void AddSignInWithAppleWithCompatibility(this ProjectCapabilityManager manager)
  16. {
  17. const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  18. var managerType = typeof(ProjectCapabilityManager);
  19. var projectField = managerType.GetField("project", bindingFlags);
  20. var entitlementFilePathField = managerType.GetField("m_EntitlementFilePath", bindingFlags);
  21. var targetGuidField = managerType.GetField("m_TargetGuid", bindingFlags);
  22. var getOrCreateEntitlementDocMethod = managerType.GetMethod("GetOrCreateEntitlementDoc", bindingFlags);
  23. if (projectField == null ||
  24. entitlementFilePathField == null ||
  25. targetGuidField == null ||
  26. getOrCreateEntitlementDocMethod == null)
  27. throw new Exception("Can't Add Sign In With Apple programatically in this Unity version.");
  28. var entitlementFilePath = entitlementFilePathField.GetValue(manager) as string;
  29. var entitlementDoc = (PlistDocument) getOrCreateEntitlementDocMethod.Invoke(manager, new object[] { });
  30. if (entitlementDoc != null)
  31. {
  32. var plistArray = new PlistElementArray();
  33. plistArray.AddString("Default");
  34. entitlementDoc.root["com.apple.developer.applesignin"] = plistArray;
  35. }
  36. var project = (PBXProject) projectField.GetValue(manager);
  37. var emptyCapability = GetEmptyCapabilityWithReflection();
  38. var mainTargetGuid = (string)targetGuidField.GetValue(manager);
  39. #if UNITY_2019_3_OR_NEWER
  40. var frameworkTargetGuid = project.GetUnityFrameworkTargetGuid();
  41. #else
  42. var frameworkTargetGuid = mainTargetGuid;
  43. #endif
  44. project.AddFrameworkToProject(frameworkTargetGuid, "AuthenticationServices.framework", true);
  45. project.AddCapability(mainTargetGuid, emptyCapability, entitlementFilePath);
  46. }
  47. private static PBXCapabilityType GetEmptyCapabilityWithReflection()
  48. {
  49. // For Unity version >= 6000.0.23f1
  50. var constructorInfo = typeof(PBXCapabilityType)
  51. .GetConstructor(
  52. BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
  53. null,
  54. new[] {typeof(bool), typeof(string), typeof(bool)},
  55. null);
  56. if (constructorInfo != null)
  57. {
  58. return (PBXCapabilityType) constructorInfo
  59. .Invoke(new object[] {true, string.Empty, true});
  60. }
  61. // For Unity version < 6000.0.23f1
  62. constructorInfo = typeof(PBXCapabilityType)
  63. .GetConstructor(
  64. BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
  65. null,
  66. new[] {typeof(string), typeof(bool), typeof(string), typeof(bool)},
  67. null);
  68. if (constructorInfo != null)
  69. {
  70. return (PBXCapabilityType) constructorInfo
  71. .Invoke(new object[] {"com.lupidan.apple-signin-unity.empty", true, string.Empty, true});
  72. }
  73. throw new Exception("Can't create empty capability in this Unity version.");
  74. }
  75. }
  76. }
  77. #endif