//添加 xcodeModel.frameworks 中指定的 framework shareSDK子平台的framework private static void AddXcodeModelFrameworks(MOBXCodeEditorModel xcodeModel, PBXProject xcodeProj, string xcodeTargetGuid, string xcodeTargetPath) { foreach (MOBPathModel pathModel in xcodeModel.frameworks) { if (pathModel.filePath.Contains(".framework") && !pathModel.filePath.Contains("MOBFoundation.framework")) { string frameworkPath = pathModel.filePath.Replace(pathModel.rootPath, ""); // Debug.Log("frameworkPath" + frameworkPath); string savePath = xcodeTargetPath + frameworkPath; int tempIndex = frameworkPath.LastIndexOf("/"); string saveFrameworkPath = frameworkPath.Substring(0, tempIndex); //将 framework copy到指定目录 DirectoryInfo frameworkInfo = new DirectoryInfo(pathModel.filePath); DirectoryInfo saveFrameworkInfo = new DirectoryInfo(savePath); CopyAll(frameworkInfo, saveFrameworkInfo); //将 framework 加入 proj中 xcodeProj.AddFileToBuild(xcodeTargetGuid, xcodeProj.AddFile(savePath, "MOB" + frameworkPath, PBXSourceTree.Sdk)); //将 build setting 设置 xcodeProj.AddBuildProperty(xcodeTargetGuid, "FRAMEWORK_SEARCH_PATHS", "$(SRCROOT)" + saveFrameworkPath); } else { Debug.LogWarning(pathModel.filePath + " no framework"); } } }
public static void onPostProcessBuild(BuildTarget target, string targetPath) { if (target != BuildTarget.iOS) { Debug.LogWarning("Target is not iPhone. XCodePostProcess will not run"); return; } //拉取配置文件中的数据 MOBXCodeEditorModel xcodeModel = new MOBXCodeEditorModel(); xcodeModel.LoadMobpds(); //导入文件 PBXProject xcodeProj = new PBXProject(); string xcodeProjPath = targetPath + "/Unity-iPhone.xcodeproj/project.pbxproj"; xcodeProj.ReadFromFile(xcodeProjPath); //xcode Target string xcodeTargetGuid = xcodeProj.TargetGuidByName("Unity-iPhone"); //plist路径targetPath string plistPath = targetPath + "/Info.plist"; PlistDocument plistDocument = new PlistDocument(); plistDocument.ReadFromFile(plistPath); PlistElementDict plistElements = plistDocument.root.AsDict(); //添加 MOBAppkey MOBAppSecret AddAPPKey(plistElements); //添加权限 等 AddPermissions(xcodeModel, plistElements); //添加白名单 AddLSApplicationQueriesSchemes(xcodeModel, plistElements); //添加 URLSchemes AddURLSchemes(xcodeModel, plistElements); //添加 InfoPlistSet AddInfoPlistSet(xcodeModel, plistElements); //写入 info.plist plistDocument.WriteToFile(plistPath); //添加 BuildSettings AddBuildSettings(xcodeModel, xcodeProj, xcodeTargetGuid); //根据配置文件加载资源及xcode设置 bool hasMobFramework = false; foreach (MOBPathModel pathModel in xcodeModel.folders) { AddFramework(pathModel.filePath, targetPath, xcodeTargetGuid, pathModel, xcodeProj, ref hasMobFramework); AddStaticLibrary(pathModel.filePath, targetPath, xcodeTargetGuid, pathModel, xcodeProj); AddHeader(pathModel.filePath, targetPath, xcodeTargetGuid, pathModel, xcodeProj); AddBundle(pathModel.filePath, targetPath, xcodeTargetGuid, pathModel, xcodeProj); AddOtherFile(pathModel.filePath, targetPath, xcodeTargetGuid, pathModel, xcodeProj, xcodeModel.fileFlags); } //加入系统Framework AddSysFrameworks(xcodeModel, xcodeProj, xcodeTargetGuid, targetPath); //加入 xcodeModel.frameworks 中指定的 framework AddXcodeModelFrameworks(xcodeModel, xcodeProj, xcodeTargetGuid, targetPath); xcodeProj.WriteToFile(xcodeProjPath); }
//添加系统Framework private static void AddSysFrameworks(MOBXCodeEditorModel xcodeModel, PBXProject xcodeProj, string xcodeTargetGuid, string xcodeTargetPath) { foreach (string sysFramework in xcodeModel.sysFrameworks) { xcodeProj.AddFrameworkToProject(xcodeTargetGuid, sysFramework, false); } }
//在info.plist中添加 白名单 private static void AddLSApplicationQueriesSchemes(MOBXCodeEditorModel xcodeModel,PlistElementDict plistElements) { ArrayList LSApplicationQueriesSchemes = xcodeModel.LSApplicationQueriesSchemes; PlistElementArray elementArray = plistElements.CreateArray ("LSApplicationQueriesSchemes"); foreach (string str in LSApplicationQueriesSchemes) { elementArray.AddString (str); } }
//在info.plist中添加 必要的权限设置 private static void AddPermissions(MOBXCodeEditorModel xcodeModel,PlistElementDict plistElements) { Hashtable permissions = xcodeModel.permissions; foreach (string key in permissions.Keys) { string value = (string)permissions[key]; plistElements.SetString(key,value); } }
//添加 BuildSettings private static void AddBuildSettings(MOBXCodeEditorModel xcodeModel , PBXProject xcodeProj,string xcodeTargetGuid) { Hashtable buildSettings = xcodeModel.buildSettings; foreach (string key in buildSettings.Keys) { ArrayList settings = (ArrayList)buildSettings[key]; foreach (string setStr in settings) { xcodeProj.AddBuildProperty(xcodeTargetGuid, key, setStr); } } }
private static void AddCapability(MOBXCodeEditorModel xcodeModel, PBXProject xcodeProj, string xcodeTargetGuid, string xcodeTargetPath) { string projectPath = PBXProject.GetPBXProjectPath(xcodeTargetPath); if (xcodeModel.isOpenRestoreScene || xcodeModel.isHaveApple) { string entitlementsPath = xcodeModel.entitlementsPath; if (entitlementsPath == null || entitlementsPath == "" || !xcodeModel.entitlementsPath.Contains(".entitlements")) { string[] s = UnityEditor.PlayerSettings.applicationIdentifier.Split('.'); string productname = s[s.Length - 1]; entitlementsPath = "Unity-iPhone/" + productname + ".entitlements"; } ProjectCapabilityManager capManager = new ProjectCapabilityManager(projectPath, entitlementsPath, PBXProject.GetUnityTargetName()); if (xcodeModel.associatedDomains.Count > 0 || xcodeModel.isHaveApple) { string[] domains = new string[xcodeModel.associatedDomains.Count]; int index = 0; foreach (string domainStr in xcodeModel.associatedDomains) { domains[index] = domainStr; index++; } capManager.AddAssociatedDomains(domains); if (xcodeModel.isHaveApple && capManager.GetType().GetMethod("AddSignInWithApple") != null) { capManager.GetType().GetMethod("AddSignInWithApple").Invoke(capManager, null); } //推送 //capManager.AddPushNotifications(true); //内购 //capManager.AddInAppPurchase(); capManager.WriteToFile(); //Debug.Log("AddCapabilityAssociatedDomains"); //Debug.Log("xcodeTargetGuid:" + xcodeTargetGuid); //Debug.Log("xcodeTargetPath:" + xcodeTargetPath); //Debug.Log("projectPath:" + projectPath); //Debug.Log("GetUnityTargetName:" + PBXProject.GetUnityTargetName()); //Debug.Log("bundleIdentifier:" + UnityEditor.PlayerSettings.applicationIdentifier); //Debug.Log("productName:" + UnityEditor.PlayerSettings.productName); xcodeProj.AddCapability(xcodeTargetGuid, PBXCapabilityType.AssociatedDomains, xcodeTargetPath + "/" + xcodeModel.entitlementsPath, true); } } }
//在info.plist中添加 URLSchemes private static void AddURLSchemes(MOBXCodeEditorModel xcodeModel,PlistElementDict plistElements) { ArrayList URLSchemes = xcodeModel.URLSchemes; PlistElementArray elementArray = plistElements.CreateArray ("CFBundleURLTypes"); foreach (Hashtable scheme in URLSchemes) { PlistElementDict dict = elementArray.AddDict (); dict.SetString ("CFBundleURLName",(string)scheme["CFBundleURLName"]); PlistElementArray urlArray = dict.CreateArray ("CFBundleURLSchemes"); ArrayList schemes = (ArrayList)scheme ["CFBundleURLSchemes"]; foreach (string schemeStr in schemes) { urlArray.AddString (schemeStr); } } }
//在info.plist中添加 infoPlistSet private static void AddInfoPlistSet(MOBXCodeEditorModel xcodeModel,PlistElementDict plistElements) { Hashtable infoPlistSets = xcodeModel.infoPlistSet; foreach (string key in infoPlistSets.Keys) { var value = infoPlistSets[key]; if(value.GetType().Equals(typeof(string))) { plistElements.SetString (key,(string)value); } else if(value.GetType().Equals(typeof(Hashtable))) { Hashtable temp = (Hashtable)value; PlistElementDict dict = plistElements.CreateDict (key); foreach (string tempKey in temp.Keys) { //暂时只支持1层dict dict.SetString (tempKey,(string)temp[tempKey]); } } } }
private static void AddCapability(MOBXCodeEditorModel xcodeModel, PBXProject xcodeProj, string xcodeTargetGuid, string xcodeTargetPath) { string projectPath = PBXProject.GetPBXProjectPath(xcodeTargetPath); if (xcodeModel.isOpenRestoreScene || xcodeModel.isHaveApple || xcodeModel.associatedDomains.Count > 0) { string entitlementsPath = xcodeModel.entitlementsPath; if (entitlementsPath == null || entitlementsPath == "" || !xcodeModel.entitlementsPath.Contains(".entitlements")) { string[] s = UnityEditor.PlayerSettings.applicationIdentifier.Split('.'); string productname = s[s.Length - 1]; entitlementsPath = "Unity-iPhone/" + productname + ".entitlements"; } #if UNITY_2018_1_OR_NEWER #if UNITY_2019_3_OR_NEWER ProjectCapabilityManager capManager = new ProjectCapabilityManager(projectPath, entitlementsPath, null, xcodeTargetGuid); #else ProjectCapabilityManager capManager = new ProjectCapabilityManager(projectPath, entitlementsPath, "Unity-iPhone"); #endif if (xcodeModel.associatedDomains.Count > 0 || xcodeModel.isHaveApple) { string[] domains = new string[xcodeModel.associatedDomains.Count]; int index = 0; foreach (string domainStr in xcodeModel.associatedDomains) { Debug.Log("AddCapabilityAssociatedDomains:" + domainStr); domains[index] = domainStr; index++; } //Debug.Log("xcodeTargetGuid:" + xcodeTargetGuid); //Debug.Log("xcodeTargetPath:" + xcodeTargetPath); //Debug.Log("projectPath:" + projectPath); //Debug.Log("GetUnityTargetName:" + PBXProject.GetUnityTargetName()); //Debug.Log("domainStr:" + MiniJSON.jsonEncode(domains)); if (capManager.GetType().GetMethod("AddAssociatedDomains") != null) { capManager.GetType().GetMethod("AddAssociatedDomains").Invoke(capManager, new object[] { domains }); } //Debug.Log("bundleIdentifier:" + UnityEditor.PlayerSettings.applicationIdentifier); //Debug.Log("productName:" + UnityEditor.PlayerSettings.productName); if (xcodeModel.isHaveApple && capManager.GetType().GetMethod("AddSignInWithApple") != null) { capManager.GetType().GetMethod("AddSignInWithApple").Invoke(capManager, null); } //推送 //capManager.AddPushNotifications(true); //内购 //capManager.AddInAppPurchase(); capManager.WriteToFile(); xcodeProj.AddCapability(xcodeTargetGuid, PBXCapabilityType.AssociatedDomains, xcodeTargetPath + "/" + entitlementsPath, true); } #else var entitlementFile = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE plist PUBLIC \" -//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"><plist version = \"1.0\"><dict>"; if (xcodeModel.associatedDomains.Count > 0 || xcodeModel.isHaveApple) { string[] domains = new string[xcodeModel.associatedDomains.Count]; int index = 0; foreach (string domainStr in xcodeModel.associatedDomains) { Debug.Log("AddCapabilityAssociatedDomains:" + domainStr); domains[index] = "<string>" + domainStr + "</string>"; index++; } var associatedDomainsString = ""; if (domains.Length > 0) { associatedDomainsString = "<key>com.apple.developer.associated-domains</key><array>" + string.Join("", domains) + "</array>"; } var appleString = ""; if (xcodeModel.isHaveApple) { appleString = "<key>com.apple.developer.applesignin</key><array><string> Default </string></array>"; } entitlementFile = entitlementFile + associatedDomainsString + appleString + "</dict></plist>"; } Debug.LogWarning(entitlementFile); Debug.LogWarning(xcodeTargetPath + "/" + entitlementsPath); Debug.LogWarning(xcodeTargetPath); StreamWriter sWriter = new StreamWriter(xcodeTargetPath + "/" + entitlementsPath); sWriter.WriteLine(entitlementFile); sWriter.Close(); sWriter.Dispose(); xcodeProj.AddBuildProperty(xcodeTargetGuid, "CODE_SIGN_ENTITLEMENTS", entitlementsPath); string fileGuid = (string)xcodeProj.AddFile(xcodeTargetPath + "/" + entitlementsPath, entitlementsPath, PBXSourceTree.Absolute); xcodeProj.AddFileToBuild(xcodeTargetGuid, fileGuid); #endif } }
//添加 Framework private static void AddFramework(string secondFilePath, string xcodeTargetPath, string xcodeTargetGuid, string xcodeFrameworkTargetGuid, MOBPathModel pathModel, PBXProject xcodeProj, ref bool hasMobFramework, MOBXCodeEditorModel editModel) { SearchOption searchOption; if (secondFilePath.Contains("/ShareSDK/") || secondFilePath.Contains("\\ShareSDK\\") || secondFilePath.Contains("/ShareSDK\\")) //shareSDK { searchOption = SearchOption.TopDirectoryOnly; } else { searchOption = SearchOption.AllDirectories; } string[] secondDirectories = Directory.GetDirectories(secondFilePath, "*.framework", searchOption); foreach (string lastFilePath in secondDirectories) { // Debug.Log("lastFilePath" + lastFilePath); int index = lastFilePath.LastIndexOf("\\"); int fileNameIndex = 2; if (index == -1) { fileNameIndex = 1; index = lastFilePath.LastIndexOf("/"); } //framework 名称 string frameworkName = lastFilePath.Substring(index + fileNameIndex); // Debug.Log("frameworkName" + frameworkName); bool isMOBFoundation = (frameworkName == "MOBFoundation.framework"); if (!isMOBFoundation || (isMOBFoundation && !hasMobFramework)) { if (isMOBFoundation && !hasMobFramework) { // Debug.Log("isMOBFoundation " + lastFilePath); hasMobFramework = true; } string frameworkPath = lastFilePath.Replace(pathModel.rootPath, ""); // Debug.Log("frameworkPath" + frameworkPath); string savePath = xcodeTargetPath + frameworkPath; int tempIndex = frameworkPath.LastIndexOf("\\"); if (tempIndex == -1) { tempIndex = frameworkPath.LastIndexOf("/"); } string saveFrameworkPath = frameworkPath.Substring(0, tempIndex); string targetGuid = xcodeFrameworkTargetGuid; //将 framework copy到指定目录AddURLSchemes DirectoryInfo frameworkInfo = new DirectoryInfo(lastFilePath); DirectoryInfo saveFrameworkInfo = new DirectoryInfo(savePath); CopyAll(frameworkInfo, saveFrameworkInfo); //将 framework 加入 proj中 if (frameworkName == "ShareSDKLink.framework" && !editModel.isOpenRestoreScene) { continue; } string fileGuid = (string)xcodeProj.AddFile(frameworkPath.Substring(1), "MOB" + frameworkPath, PBXSourceTree.Absolute); string[] dynamicframework = { "OasisSDK.framework", "SCSDKCreativeKit.framework", "SCSDKLoginKit.framework", "SCSDKCoreKit.framework" }; int dynamicIndex = Array.IndexOf(dynamicframework, frameworkName); if (dynamicIndex >= 0) { targetGuid = xcodeTargetGuid; xcodeProj.AddFileToBuildSection(targetGuid, xcodeProj.AddFrameworksBuildPhase(targetGuid), fileGuid); #if UNITY_2019_3_OR_NEWER xcodeProj.AddFileToEmbedFrameworks(targetGuid, fileGuid, targetGuid); #endif } else { xcodeProj.AddFileToBuildSection(targetGuid, xcodeProj.AddFrameworksBuildPhase(targetGuid), fileGuid); } //将 build setting 设置 xcodeProj.AddBuildProperty(targetGuid, "FRAMEWORK_SEARCH_PATHS", "$(SRCROOT)" + saveFrameworkPath.Replace("\\", "/")); } } }
public static void onPostProcessBuild(BuildTarget target, string targetPath) { MobAppKey = ""; if (target != BuildTarget.iOS) { Debug.LogWarning("Target is not iPhone. XCodePostProcess will not run"); return; } initMobAppKeyInfo(); //拉取配置文件中的数据 MOBXCodeEditorModel xcodeModel = new MOBXCodeEditorModel(); xcodeModel.MobAppKey = MobAppKey; xcodeModel.LoadMobpds(); platformJsList = xcodeModel.platformJsList; //导入文件 PBXProject xcodeProj = new PBXProject(); string xcodeProjPath = targetPath + "/Unity-iPhone.xcodeproj/project.pbxproj"; xcodeProj.ReadFromFile(xcodeProjPath); //xcode Target string xcodeTargetGuid = null; string xcodeFrameworkTargetGuid = null; #if UNITY_2019_3_OR_NEWER xcodeFrameworkTargetGuid = xcodeProj.GetUnityFrameworkTargetGuid(); xcodeTargetGuid = xcodeProj.GetUnityMainTargetGuid(); xcodeProj.AddFileToBuild(xcodeTargetGuid, xcodeProj.GetTargetProductFileRef(xcodeFrameworkTargetGuid)); #else xcodeTargetGuid = xcodeProj.TargetGuidByName("Unity-iPhone"); xcodeFrameworkTargetGuid = xcodeTargetGuid; #endif xcodeProj.AddBuildProperty(xcodeFrameworkTargetGuid, "ENABLE_BITCODE", "NO"); //plist路径targetPath string plistPath = targetPath + "/Info.plist"; PlistDocument plistDocument = new PlistDocument(); plistDocument.ReadFromFile(plistPath); PlistElementDict plistElements = plistDocument.root.AsDict(); //添加 MOBAppkey MOBAppSecret AddAPPKey(plistElements); //添加权限 等 AddPermissions(xcodeModel, plistElements); //添加白名单 AddLSApplicationQueriesSchemes(xcodeModel, plistElements); //添加 URLSchemes AddURLSchemes(xcodeModel, plistElements); //添加 InfoPlistSet AddInfoPlistSet(xcodeModel, plistElements); //写入 info.plist plistDocument.WriteToFile(plistPath); //添加 BuildSettings AddBuildSettings(xcodeModel, xcodeProj, xcodeTargetGuid); //根据配置文件加载资源及xcode设置 bool hasMobFramework = false; foreach (MOBPathModel pathModel in xcodeModel.folders) { AddFramework(pathModel.filePath, targetPath, xcodeTargetGuid, xcodeFrameworkTargetGuid, pathModel, xcodeProj, ref hasMobFramework, xcodeModel); AddStaticLibrary(pathModel.filePath, targetPath, xcodeFrameworkTargetGuid, pathModel, xcodeProj); AddHeader(pathModel.filePath, targetPath, xcodeTargetGuid, pathModel, xcodeProj); AddBundle(pathModel.filePath, targetPath, xcodeTargetGuid, pathModel, xcodeProj); AddOtherFile(pathModel.filePath, targetPath, xcodeTargetGuid, pathModel, xcodeProj, xcodeModel.fileFlags); } #if UNITY_2018_1_OR_NEWER #else AddCapability(xcodeModel, xcodeProj, xcodeTargetGuid, targetPath); #endif //加入系统Framework AddSysFrameworks(xcodeModel, xcodeProj, xcodeFrameworkTargetGuid, targetPath); //加入 xcodeModel.frameworks 中指定的 framework AddXcodeModelFrameworks(xcodeModel, xcodeProj, xcodeFrameworkTargetGuid, targetPath); xcodeProj.WriteToFile(xcodeProjPath); #if UNITY_2018_1_OR_NEWER AddCapability(xcodeModel, xcodeProj, xcodeTargetGuid, targetPath); #endif }