XCodePostProcessBuild.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #if UNITY_IOS || UNITY_TVOS
  2. #define UNITY_XCODE_EXTENSIONS_AVAILABLE
  3. #endif
  4. using UnityEditor;
  5. using System.IO;
  6. using UnityEngine;
  7. #if UNITY_IOS && UNITY_EDITOR
  8. using UnityEditor.Callbacks;
  9. using UnityEditor.iOS.Xcode;
  10. #endif
  11. using AppleAuth.Editor;
  12. #if UNITY_XCODE_EXTENSIONS_AVAILABLE
  13. //using UnityEditor.iOS.Xcode;
  14. #endif
  15. public static class XCodePostProcessBuild
  16. {
  17. #if UNITY_IOS && UNITY_EDITOR
  18. private static readonly string[] csAddFrameworks = new string[]{
  19. "Security.framework","WebKit.framework", "CoreGraphics.framework"
  20. };
  21. [PostProcessBuild(1)]
  22. public static void OnPostprocessBuild(BuildTarget target, string path)
  23. {
  24. if (BuildTarget.iOS != target)
  25. {
  26. return;
  27. }
  28. string projectPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
  29. SetFrameworksAndBuildSettings(projectPath);
  30. SetInfoList(path, "com.xmjssvr.BowArrow", "wxfe29f3f64e3c5d16");
  31. SetAssociatedDomains(projectPath, "xmjssvr.cn");
  32. setSignInWithApple(projectPath);
  33. }
  34. private static void SetFrameworksAndBuildSettings(string path)
  35. {
  36. PBXProject proj = new PBXProject();
  37. proj.ReadFromString(File.ReadAllText(path));
  38. string target = proj.GetUnityMainTargetGuid();
  39. string frameworkGuid = proj.GetUnityFrameworkTargetGuid();
  40. Debug.Log("Target Name is " + target);
  41. // 设置 BuildSettings
  42. //proj.AddBuildProperty(target, "Other Linker Flags", "-Objc -all_load");
  43. // 添加 Other Linker Flags
  44. proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-ObjC");
  45. proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-all_load");
  46. proj.AddBuildProperty(frameworkGuid, "OTHER_LDFLAGS", "-ObjC");
  47. proj.AddBuildProperty(frameworkGuid, "OTHER_LDFLAGS", "-all_load");
  48. proj.SetBuildProperty(target, "ENABLE_BITCODE", "NO");
  49. proj.SetBuildProperty(frameworkGuid, "ENABLE_BITCODE", "NO");
  50. //根据微信SDK文档的要求,加入相关的Frameworks
  51. for (int i = 0; i < csAddFrameworks.Length; ++i)
  52. {
  53. if (!proj.ContainsFramework(target, csAddFrameworks[i]))
  54. proj.AddFrameworkToProject(target, csAddFrameworks[i], false);
  55. }
  56. File.WriteAllText(path, proj.WriteToString());
  57. }
  58. public static void SetInfoList(string buildPath, string wxUrlName, string wxScheme)
  59. {
  60. string listPath = buildPath + "/Info.plist";
  61. PlistDocument plist = new PlistDocument();
  62. plist.ReadFromString(File.ReadAllText(listPath));
  63. // 在“info”标签栏的“URL type“添加“URL scheme”,值为你在微信后台注册的应用程序的 AppID
  64. PlistElementArray urlArray = plist.root.CreateArray("CFBundleURLTypes");
  65. PlistElementDict dict = urlArray.AddDict();
  66. dict.SetString("CFBundleTypeRole", "Editor");
  67. dict.SetString("CFBundleURLName", wxUrlName);
  68. PlistElementArray urlSchemes = dict.CreateArray("CFBundleURLSchemes");
  69. urlSchemes.AddString(wxScheme);
  70. // 在 “info”标签栏的“LSApplicationQueriesSchemes“添加weixin wechat和weixinULAPI
  71. PlistElementArray wxArray = plist.root.CreateArray("LSApplicationQueriesSchemes");
  72. wxArray.AddString("weixin");
  73. //wxArray.AddString("wechat");
  74. wxArray.AddString("weixinULAPI");
  75. wxArray.AddString("weixinURLParamsAPI");
  76. PlistElementDict ed = plist.root;
  77. ed.SetString("NSBluetoothAlwaysUsageDescription", "您是否允此App使用蓝牙,来搜索和连接设备?");
  78. ed.SetString("NSBluetoothPeripheralUsageDescription", "您是否允此App使用蓝牙,来搜索和连接设备?");
  79. ed.SetString("NSCameraUsageDescription", "App需要您的同意,才能访问照相机");
  80. ed.SetString("NSPhotoLibraryUsageDescription", "App需要您的同意,才能访问相册");
  81. ed.SetString("NSLocationWhenInUseUsageDescription", "我们需要通过您的地理位置信息获取您周边的相关数据");
  82. ed.SetString("NSLocationAlwaysUsageDescription", "我们需要通过您的地理位置信息获取您周边的相关数据");
  83. File.WriteAllText(listPath, plist.WriteToString());
  84. }
  85. // 设置Associated Domains
  86. public static void SetAssociatedDomains(string pbxProjectPath, string domainUrl)
  87. {
  88. //默认 Target Name, 你自己的可能不一样
  89. string targetName = "Unity-iPhone";
  90. //Set the entitlements file name to what you want but make sure it has this extension
  91. string entitlementsFileName = "my_app.entitlements";
  92. var entitlements = new ProjectCapabilityManager(pbxProjectPath, entitlementsFileName, targetName);
  93. entitlements.AddAssociatedDomains(new string[] { "applinks:" + domainUrl });
  94. entitlements.WriteToFile();
  95. }
  96. // 设置Associated Domains
  97. public static void setSignInWithApple(string pbxProjectPath)
  98. {
  99. //默认 Target Name, 你自己的可能不一样
  100. string targetName = "Unity-iPhone";
  101. //Set the entitlements file name to what you want but make sure it has this extension
  102. string entitlementsFileName = "my_app.entitlements";
  103. var project = new PBXProject();
  104. project.ReadFromString(System.IO.File.ReadAllText(pbxProjectPath));
  105. var entitlements = new ProjectCapabilityManager(pbxProjectPath, entitlementsFileName, targetName, project.GetUnityMainTargetGuid());
  106. //entitlements.AddSignInWithAppleWithCompatibility(project.GetUnityFrameworkTargetGuid());
  107. entitlements.AddSignInWithAppleWithCompatibility();
  108. entitlements.WriteToFile();
  109. }
  110. #endif
  111. }