Xcode How to add a run script build phase

In case there is the need to do some actions  “automatically”  while building an app we can attach a shell script to a “build phase“. All Xcode targets have a chain of phases which are executed in order to produce the final build . This chain can be changed adding extra actions.
Examples of uses :
copy files in other location after/before build
– create pkg .app (zip/tar) and move package to the staging directory
update build info (build version, number and date)
– …

Step #1

Create a new phase :
open Xcode project
select compile “target” (under “Targets” group for example : myClient)
right click on “myClient” target then select from menu : Add” -> “New Build Phase” -> “New Run Script Build Phase

"Add" -> "New Build Phase"
“Add” -> “New Build Phase”

Consider that actions (phases) are executed in order as they are listed. In our example (see below screenshot):
1. Run Script – Build version
2. Run Script – CP

Actions are executed in order
Actions are executed in order

So you need to move the script (up/down) in the position where you need (example before or after link phase or compile…).

Step #2

Double click on “Run Script” opens the “editor“. Here you can write directly your schell script (in text area under Script) .
 Note : here your script sees all Xcode environment variables such as src path : ${SRCROOT}
For Build Setting Reference (TARGET_BUILD_DIR, SRCROOT …) click here.

Editor for script phase
Editor for script phase


As example we  create an about.html page within all information about our application such as build number and date of build.
for example the build_string : “Build 1.0.189 (2011-05-26)
Some notes about file about.html:

  • about.html is a project resource which is displayed in a UIVewView (for example selecting button “about” in your application).
  • about.html contains a lot of HTML text so we need to “include” separately the build_string which change at every build

Note : we wont change/create the txt directly in about.html
We can edit about.html in order to include a second file info.html (auto generated by the script).
So info.html will contain only build information : <font ….> Build 1.0.189 (2011-05-26) </font>
Those files (about.html and info.html) have to be added to project as resources. Really info.html will be regenerate, using a shell script, every time you compile. When UIVewView will load about.html the file info.html will be included in iframe.

So we have two files :
 Static file about.html contains :

    <strong>My Client</strong>
    <iframe src="info.html" frameborder="no" width=300 height=40></iframe>

2. Auto generated file info.html
as said info.html is the file generated automatically every time the application is compiled. In order to generate info.html :

  • create a phase as script (described above)
  • write the below code (this code will generate info.html)
awk '{print ($1+1);}' ${SRCROOT}/Resources/extra/buildinfo.txt >${SRCROOT}/buildinfo.tmp
mv ${SRCROOT}/buildinfo.tmp ${SRCROOT}/Resources/extra/buildinfo.txt
echo "<HTML><font border=0 size=2px color=#999999>Build 1.0.cat ${SRCROOT}/Resources/extra/buildinfo.txt (date +%Y-%m-%d)</font></HTML>" > ${SRCROOT}/Resources/extra/info.html

Screen shot 2011-05-26 at 5.59.31 PMNote : file buildinfo.txt is a support file where is stored just last build number (is just a counter in order to store the last build number).

User Review
0 (0 votes)