Unity 导出Xcode 自动添加framework
//打包完成后调用的方法
...
[PostProcessBuild(1)]
public static void AfterBuild(BuildTarget target, string pathToBuiltProject)
{
if (target == BuildTarget.iOS)
{
BuildIOS(target, pathToBuiltProject);
}
}
private static void BuildIOS(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iOS)
{
return;
}
var projPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
var proj = new PBXProject();
proj.ReadFromFile(projPath);
var targetGUID = "";
var mainTargetGUID = "";
//#if UNITY_2019_3_OR_NEWER
targetGUID = proj.GetUnityFrameworkTargetGuid();
mainTargetGUID = proj.GetUnityMainTargetGuid();
//#else
targetGUID = proj.TargetGuidByName("Unity-iPhone");
mainTargetGUID = targetGUID;
//#endif
AddBuildProperty(proj, targetGUID, mainTargetGUID);
AddOtherBuildFramework(proj, targetGUID, mainTargetGUID);
proj.WriteToFile(projPath);
// 修改Info.plist文件
string plistPath = Path.Combine(pathToBuiltProject, "Info.plist");
PlistDocument plist = new PlistDocument();
plist.ReadFromFile(plistPath);
// 添加微信信息
var weixinArray = plist.root.CreateArray("LSApplicationQueriesSchemes");
weixinArray.AddString("weixin");
weixinArray.AddString("weixinULAPI");
weixinArray.AddString("weixinURLParamsAPI");
//<key>ITSAppUsesNonExemptEncryption</key><false/>
plist.root.SetBoolean("ITSAppUsesNonExemptEncryption", false);
// 设置获取IDFA弹窗文案
plist.root.SetString("NSUserTrackingUsageDescription", "为给您更精准的提供个性化内容,请您允许我们获取广告信息");
var skArray = plist.root.CreateArray("SKAdNetworkItems");
var itemDic = skArray.AddDict();
itemDic.SetString("SKAdNetworkIdentifier", "238da6jt44.skadnetwork");
var urlType = plist.root.CreateArray("CFBundleURLTypes");
// 添加微信 APPID
var urlTypeDic = urlType.AddDict();
urlTypeDic.SetString("CFBundleTypeRole", "Editor");
urlTypeDic.SetString("CFBundleURLName", "weixin");
var urlTypeDicArray = urlTypeDic.CreateArray("CFBundleURLSchemes");
LoadConfig();
urlTypeDicArray.AddString(projectID.m_wxAppID);
plist.WriteToFile(plistPath);
}
// 添加Framework
private static void AddBuildProperty(PBXProject proj, string targetGUID, string mainTargetGUID)
{
proj.AddBuildProperty(targetGUID, "OTHER_LDFLAGS", "-ObjC");
proj.AddBuildProperty(targetGUID, "OTHER_LDFLAGS", "-force_load");
proj.AddBuildProperty(targetGUID, "OTHER_LDFLAGS", "$(PROJECT_DIR)/UnityFramework/ABUAdCsjAdapter.framework/ABUAdCsjAdapter");
proj.AddBuildProperty(targetGUID, "OTHER_LDFLAGS", "-Ic++abi");
proj.SetBuildProperty(targetGUID, "ENABLE_BITCODE", "NO");
proj.SetBuildProperty(targetGUID, "GCC_ENABLE_OBJC_EXCEPTIONS", "YES");
proj.SetBuildProperty(mainTargetGUID, "GCC_ENABLE_OBJC_EXCEPTIONS", "YES");
// framework to project
proj.AddFrameworkToProject(targetGUID, "WebKit.framework", false);
proj.AddFrameworkToProject(targetGUID, "VideoToolbox.framework", false);
proj.AddFrameworkToProject(targetGUID, "SystemConfiguration.framework", false);
proj.AddFrameworkToProject(targetGUID, "StoreKit.framework", false);
proj.AddFrameworkToProject(targetGUID, "Security.framework", false);
proj.AddFrameworkToProject(targetGUID, "ReplayKit.framework", false);
proj.AddFrameworkToProject(targetGUID, "Photos.framework", false);
proj.AddFrameworkToProject(targetGUID, "MultipeerConnectivity.framework", false);
proj.AddFrameworkToProject(targetGUID, "MobileCoreServices.framework", false);
proj.AddFrameworkToProject(targetGUID, "MetalPerformanceShaders.framework", false);
proj.AddFrameworkToProject(targetGUID, "MediaToolbox.framework", false);
proj.AddFrameworkToProject(targetGUID, "MediaPlayer.framework", false);
proj.AddFrameworkToProject(targetGUID, "JavaScriptCore.framework", false);
proj.AddFrameworkToProject(targetGUID, "GLKit.framework", false);
proj.AddFrameworkToProject(targetGUID, "CoreTelephony.framework", false);
proj.AddFrameworkToProject(targetGUID, "AVFoundation.framework", false);
proj.AddFrameworkToProject(targetGUID, "AudioToolbox.framework", false);
proj.AddFrameworkToProject(targetGUID, "AssetsLibrary.framework", false);
proj.AddFrameworkToProject(targetGUID, "Accelerate.framework", false);
proj.AddFrameworkToProject(targetGUID, "AuthenticationServices.framework", false);
proj.AddFrameworkToProject(targetGUID, "AuthenticationServices.framework", false);
proj.AddFrameworkToProject(targetGUID, "CoreText.framework", false);
proj.AddFrameworkToProject(targetGUID, "AVKit.framework", false);
proj.AddFrameworkToProject(targetGUID, "CFNetwork.framework", false);
proj.AddFrameworkToProject(targetGUID, "CoreGraphics.framework", false);
proj.AddFrameworkToProject(targetGUID, "CoreMedia.framework", false);
proj.AddFrameworkToProject(targetGUID, "CoreMotion.framework", false);
proj.AddFrameworkToProject(targetGUID, "CoreVideo.framework", false);
proj.AddFrameworkToProject(targetGUID, "Foundation.framework", false);
proj.AddFrameworkToProject(targetGUID, "OpenAL.framework", false);
proj.AddFrameworkToProject(targetGUID, "OpenGLES.framework", false);
proj.AddFrameworkToProject(targetGUID, "QuartzCore.framework", false);
proj.AddFrameworkToProject(targetGUID, "UIKit.framework", false);
proj.AddFrameworkToProject(targetGUID, "Metal.framework", false);
proj.AddFrameworkToProject(targetGUID, "CoreLocation.framework", false);
proj.AddFrameworkToProject(targetGUID, "ImageIO.framework", false);
proj.AddFrameworkToProject(targetGUID, "AdSupport.framework", false);
proj.AddFrameworkToProject(targetGUID, "AppTrackingTransparency.framework", false);
proj.AddFrameworkToProject(targetGUID, "DeviceCheck.framework", false);
proj.AddFrameworkToProject(targetGUID, "CoreImage.framework", false);
proj.AddFrameworkToProject(mainTargetGUID, "UnityFramework.framework", false);
}
private static void AddOtherBuildFramework(PBXProject proj, string targetGUID,string mainTargetGUID)
{
proj.AddFrameworkToProject(targetGUID, "libsqlite3.tbd", false);
proj.AddFrameworkToProject(targetGUID, "libresolv.9.tbd", false);
proj.AddFrameworkToProject(targetGUID, "libiconv.tbd", false);
proj.AddFrameworkToProject(targetGUID, "libcompression.tbd", false);
proj.AddFrameworkToProject(targetGUID, "libc++abi.tbd", false);
proj.AddFrameworkToProject(targetGUID, "libil2cpp.a", false);
proj.AddFrameworkToProject(targetGUID, "libiPhone-lib.a", false);
proj.AddFrameworkToProject(targetGUID, "libiconv.2.dylib", false);
proj.AddFrameworkToProject(targetGUID, "libresolv.9.tbd", false);
proj.AddFrameworkToProject(targetGUID, "libc++.tbd", false);
proj.AddFrameworkToProject(targetGUID, "libz.tbd", false);
proj.AddFrameworkToProject(targetGUID, "libbz2.tbd", false);
proj.AddFrameworkToProject(targetGUID, "libxml2.tbd", false);
}
// 添加关联域
static void AddAssociatedDomains()
{
var projPath = PBXProject.GetPBXProjectPath(path);
var proj = new PBXProject();
proj.ReadFromFile(projPath);
//var manager = new ProjectCapabilityManager(projPath, "Entitlements.entitlements", null, proj.GetUnityMainTargetGuid());
var manager = new ProjectCapabilityManager(
projPath,
"Entitlements.entitlements",
targetGuid: proj.GetUnityMainTargetGuid()
);
string universal = projectID.Universal_Links.Replace("https://", "applink:");
string[] str = { universal.Substring(0,universal.Length-1) };
manager.AddAssociatedDomains(str);
manager.WriteToFile();
}
...