Search code examples
iosxcodemacosbuild-processrun-script

How can I programmatically add a run script build phase to an Xcode project?


I am wondering if it is possible to add a run script build phase to another Xcode project from within my Mac utility app. I can see what takes place in an Xcode project when a run script is added (doing a diff), but I don't know how to safely add the equivalent in code.

Am I supposed to parse it manually? Is there documentation or libraries available for this? This would be a useful feature if I can make sure I do it safely and right. I don't want to be messing up people's Xcode projects!


Solution

  • I actually decided to add a run script. It turned out to be quite easy. First, in Xcode, add a run script to your project and observe the changes via the diff tool. There is an addition to the 'buildPhases' array (which is nested down pretty deep in some arbitrarily named dictionaries), and then the key that was added to the 'buildPhases' array was also added to the 'objects' dictionary with a dictionary that represents the actual run script build phase format.

    So to replicate this programmatically, get the contents of the pbxproj file into an NSDictionary, and iterate through the dictionaries looking for the buildPhases array. Then, add your key. Finally, add an object to the 'objects' array with the key and a dictionary that mirrors the format in Xcode.

    Save the file back to disk in the same location and voila! You have programmatically added a run script build phase.