Skip to content

Instantly share code, notes, and snippets.

@sryze
Created March 8, 2017 04:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sryze/eee776e85813f45f84a33441f1d10cdf to your computer and use it in GitHub Desktop.
Save sryze/eee776e85813f45f84a33441f1d10cdf to your computer and use it in GitHub Desktop.
Modify Xcode project after build in Unity
#if UNITY_EDITOR
using System;
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.XcodeNew;
public class PostBuild {
[PostProcessBuildAttribute]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) {
#if UNITY_IOS
var projectPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
var project = new PBXProject();
project.ReadFromFile(projectPath);
var infoPlist = new PlistDocument();
var infoPlistPath = pathToBuiltProject + "/Info.plist";
infoPlist.ReadFromFile(infoPlistPath);
AddFrameworks(project);
CopyPodfile(pathToBuiltProject);
ConfigureFabric(infoPlist, project);
infoPlist.WriteToFile(infoPlistPath);
project.WriteToFile(projectPath);
#endif
}
private static string GetDefaultTarget(PBXProject project) {
return project.TargetGuidByName(PBXProject.GetUnityTargetName());
}
private static void AddFrameworks(PBXProject project) {
Debug.Log("Adding frameworks");
var defaultTarget = GetDefaultTarget(project);
project.AddFrameworkToProject(defaultTarget, "AdSupport.framework", false);
}
private static void CopyPodfile(string pathToBuiltProject) {
var podfilePath = "Assets/BoxLoader/iOS/Podfile";
var destPodfilePath = pathToBuiltProject + "/Podfile";
Debug.Log(String.Format("Copying Podfile from {0} to {1}", podfilePath, destPodfilePath));
if (!File.Exists(destPodfilePath)) {
FileUtil.CopyFileOrDirectory(podfilePath, destPodfilePath);
} else {
Debug.Log("Podfile already exists");
}
}
private static void ConfigureFabric(PlistDocument infoPlist, PBXProject project) {
Debug.Log("Adding Fabric info to Info.plist");
const string FabricApiKey = "<YOUR API KEY>";
const string FabricSecret = "<YOUR SECRET>";
var fabricDict = new PlistElementDict();
fabricDict.SetString("APIKey", FabricApiKey);
var fabricKitsArray = fabricDict.CreateArray("Kits");
var crashlyticsKit = fabricKitsArray.AddDict();
crashlyticsKit.SetString("KitName", "Crashlytics");
crashlyticsKit.CreateDict("KitInfo");
infoPlist.root["Fabric"] = fabricDict;
Debug.Log("Adding a run script phase for Fabric");
var defaultTarget = GetDefaultTarget(project);
var fabricScript = project.ShellScriptByName(defaultTarget, "Fabric");
if (fabricScript == null) {
fabricScript = String.Format("\"${{PODS_ROOT}}/Fabric/run\" {0} {1}", FabricApiKey, FabricSecret);
project.AppendShellScriptBuildPhase(defaultTarget, "Fabric", "", fabricScript);
}
}
}
#endif
@dsarfati
Copy link

dsarfati commented Feb 1, 2018

Where does project.ShellScriptByName and AppendShellScriptBuildPhase come from? I dont see that in the current docs https://docs.unity3d.com/ScriptReference/iOS.Xcode.PBXProject.html

@alickmail
Copy link

alickmail commented Feb 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment