public static void OnPostprocessBuild(string xcodePath, string configPath) { pluginPath = configPath.Replace("XcodeSetting.json", ""); string projPath = PBXProject.GetPBXProjectPath(xcodePath); PBXProject proj = new PBXProject(); proj.ReadFromString(File.ReadAllText(projPath)); string plistPath = xcodePath + "/Info.plist"; PlistDocument plist = new PlistDocument(); plist.ReadFromString(File.ReadAllText(plistPath)); PlistElementDict rootDict = plist.root; //string target = proj.TargetGuidByName(PBXProject.GetUnityTargetName()); //string target = proj.TargetGuidByName("UnityFramework"); //string productName = proj.GetBuildPropertyForAnyConfig(target, "PRODUCT_NAME"); string entitlementFilePath = Path.Combine(PBXProject.GetUnityTargetName(), "usdk.entitlements"); ProjectCapabilityManager pcbManager = new ProjectCapabilityManager(proj, projPath, entitlementFilePath, PBXProject.GetUnityTargetName()); //读取配置文件 string json = File.ReadAllText(configPath); Hashtable table = json.hashtableFromJson(); embedFrameworksTable = table.SGet <Hashtable>("embedframeworks"); //plist SetPlist(proj, rootDict, table.SGet <Hashtable>("plist")); plist.WriteToFile(plistPath); //lib SetLibs(proj, table.SGet <Hashtable>("libs")); //framework SetFrameworks(proj, table.SGet <Hashtable>("frameworks")); //building setting SetBuildProperties(proj, table.SGet <Hashtable>("properties")); SetShellScriptBuildPhase(proj, table.SGet <Hashtable>("shellscript")); //复制文件 CopyFiles(proj, xcodePath, table.SGet <Hashtable>("files")); //复制文件夹 CopyFolders(proj, xcodePath, table.SGet <Hashtable>("folders")); //文件编译符号 SetFilesCompileFlag(proj, table.SGet <Hashtable>("filesCompileFlag")); //加入能力 SetCapabilitys(pcbManager, table.SGet <Hashtable>("capabilitys")); //写入 File.WriteAllText(projPath, proj.WriteToString()); Console.WriteLine("***Info.plist*****\n" + File.ReadAllText(plistPath)); }
/// <summary> /// 设置能力 /// </summary> /// <param name="proj"></param> /// <param name="arg"></param> private static void SetCapabilitys(ProjectCapabilityManager pcbManager, Hashtable arg) { if (arg == null) { return; } foreach (DictionaryEntry i in arg) { string capabilityName = i.Key.ToString(); ArrayList args = i.Value as ArrayList; AddCapabilitys(pcbManager, capabilityName, args.ToArray()); Console.WriteLine(string.Format("Add '{0}' Capability.", capabilityName)); } pcbManager.WriteToFile(); Console.WriteLine(string.Format("Add {0} Capabilitys Success.", arg.Count)); }
private static void AddCapabilitys(ProjectCapabilityManager pcbManager, string capabilityName, object[] args) { switch (capabilityName) { case "ApplePay": pcbManager.AddApplePay(args as string[]); break; case "AppGroups": pcbManager.AddAppGroups(args as string[]); break; case "AssociatedDomains": pcbManager.AddAssociatedDomains(args as string[]); break; case "BackgroundModes": BackgroundModesOptions ops = BackgroundModesOptions.None; foreach (string op in args) { BackgroundModesOptions bmop; if (Enum.TryParse(op, out bmop)) { ops = ops | bmop; } } pcbManager.AddBackgroundModes(ops); break; case "DataProtection": pcbManager.AddDataProtection(); break; case "GameCenter": pcbManager.AddGameCenter(); break; case "HealthKit": pcbManager.AddHealthKit(); break; case "HomeKit": pcbManager.AddHomeKit(); break; case "iCloud": bool enableKeyValueStorage = Convert.ToBoolean(args[0]); bool enableiCloudDocument = Convert.ToBoolean(args[1]); List <string> customContainers = new List <string>(); if (args.Length > 2) { for (int i = 2; i < args.Length - 1; i++) { customContainers.Add(args[i].ToString()); } } pcbManager.AddiCloud(enableKeyValueStorage, enableiCloudDocument, customContainers.ToArray()); break; case "InAppPurchase": pcbManager.AddInAppPurchase(); break; case "InterAppAudio": pcbManager.AddInterAppAudio(); break; case "KeychainSharing": pcbManager.AddKeychainSharing(args as string[]); break; case "Maps": MapsOptions mops = MapsOptions.None; foreach (string op in args) { MapsOptions mop; if (Enum.TryParse(op, out mop)) { mops = mops | mop; } } pcbManager.AddMaps(mops); break; case "PersonalVPN": pcbManager.AddPersonalVPN(); break; case "PushNotifications": bool development = Convert.ToBoolean(args[0]); pcbManager.AddPushNotifications(development); break; case "Siri": pcbManager.AddSiri(); break; case "Wallet": pcbManager.AddWallet(args as string[]); break; case "WirelessAccessoryConfiguration": pcbManager.AddWirelessAccessoryConfiguration(); break; case "SignInWithApple": pcbManager.AddSignInWithApple(); break; case "AccessWiFiInformation": pcbManager.AddAccessWiFiInformation(); break; } }
public void OnPostprocessBuild(BuildTarget buildTarget, string pathToBuiltProject) { // Stop processing if targe is NOT iOS if (buildTarget != BuildTarget.iOS) { return; } // Initialize PbxProject var projectPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj"; PBXProject pbxProject = new PBXProject(); pbxProject.ReadFromFile(projectPath); string targetGuid = pbxProject.TargetGuidByName("Unity-iPhone"); // Sample of adding build property // pbxProject.AddBuildProperty (targetGuid, "OTHER_LDFLAGS", "-all_load"); // Sample of setting build property // pbxProject.SetBuildProperty (targetGuid, "ENABLE_BITCODE", "NO"); // Sample of update build property // pbxProject.UpdateBuildProperty (targetGuid, "OTHER_LDFLAGS", new string[] { "-ObjC" }, new string[] { "-weak_framework" }); // Sample of adding REQUIRED framwrok // pbxProject.AddFrameworkToProject (targetGuid, " Security.framework ", false); // Sample of adding OPTIONAL framework // pbxProject.AddFrameworkToProject (targetGuid, " SafariServices.framework ", true); // Sample of setting compile flags pbxProject.AddCapability(targetGuid, PBXCapabilityType.InAppPurchase); pbxProject.AddCapability(targetGuid, PBXCapabilityType.iCloud); pbxProject.AddCapability(targetGuid, PBXCapabilityType.GameCenter); pbxProject.AddCapability(targetGuid, PBXCapabilityType.PushNotifications); // pbxProject.SetBuildProperty(targetGuid, "DEVELOPMENT_TEAM", "<set developement key here>"); var guid = pbxProject.FindFileGuidByProjectPath("Classes/UI/Keyboard.mm"); var flags = pbxProject.GetCompileFlagsForFile(targetGuid, guid); flags.Add("-fno-objc-arc"); pbxProject.SetCompileFlagsForFile(targetGuid, guid, flags); pbxProject.AddFrameworkToProject(targetGuid, "CloudKit.framework", false); File.WriteAllText(projectPath, pbxProject.WriteToString()); // Samlpe of editing Info.plist var plistPath = Path.Combine(pathToBuiltProject, "Info.plist"); var plist = new PlistDocument(); plist.ReadFromFile(plistPath); // Add string setting plist.root.SetBoolean("ITSAppUsesNonExemptEncryption", false); // Add URL Scheme\ // Apply editing settings to Info.plist var cap = plist.root.CreateArray("UIRequiredDeviceCapabilities"); cap.AddString("gamekit"); plist.WriteToFile(plistPath); //----- var file_name = "unity.entitlements"; var proj_path = projectPath; var proj = new PBXProject(); proj.ReadFromFile(proj_path); // target_name = "Unity-iPhone" var target_name = PBXProject.GetUnityTargetName(); var target_guid = proj.TargetGuidByName(target_name); var dst = pathToBuiltProject + "/" + target_name + "/" + file_name; try { File.WriteAllText(dst, entitlements); proj.AddFile(target_name + "/" + file_name, file_name); proj.AddBuildProperty(target_guid, "CODE_SIGN_ENTITLEMENTS", target_name + "/" + file_name); proj.WriteToFile(proj_path); } catch (IOException e) { Debug.Log("Could not copy entitlements. Probably already exists. " + e); } UnityEditor.iOS.Xcode.ProjectCapabilityManager pcm = new UnityEditor.iOS.Xcode.ProjectCapabilityManager(proj_path, dst, target_name); pcm.AddPushNotifications(false); }