XCodePostProcessBuild.cs 4.3 KB

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