iPhone App Version Information In XCode

Mon 17 January 2011, tagged: C++

When you create an iOS Application in XCode by default the version of the application is stored in-Info.plist. It would be really handy to get this in to code for compile time checks and #defines.

PList files are actually XML files which makes them easy to read in a shell script but complex to manipulate in bash. Luckily there is a utility called PListBuddy installed in /usr/libexec (not the usual path for command line utilities in OS X) which can manipulate PList files in various ways. For example:

1
/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" Stranded/Stranded-Info.plist

outputs

1
0.28

showing that the current development version of stranded is 0.28 (long way to go to get to 1.0)

I wanted to output a header file called version.h from my various targets based upon the value of CFBundleVersion in Stranded-Info.plist. To accomplish this I added a small build step to each target which ran the following script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
PLISTBUDDY='/usr/libexec/PlistBuddy'
PFILE='Stranded-Info.plist'
VERSIONFILE='Classes/version.h'
version=$($PLISTBUDDY -c 'Print :CFBundleVersion' $PFILE)
IFS="." arr=( $version )
echo "#ifndef VERSION_H" > $VERSIONFILE
echo "#define VERSION_H" > $VERSIONFILE
echo "#define MAJOR_VERSION ${arr[0]}" > $VERSIONFILE
echo "#define MINOR_VERSION ${arr[1]}" > $VERSIONFILE 
echo "#endif" > $VERSIONFILE 
echo "" > $VERSIONFILE

This should be placed as a ‘New Run Script Build Phase’ with your info.plist file as input and your output file as the version.h file you want to generate.

Run Script Phase

It’s important that this build script is the first script to run so that version.h is updated BEFORE the application code is built.

Target View

I’ve renamed the build script to “Update Version” so I remember what it is.

Now in code you can write:

1
2
3
#include "version.h"
... 
HUD::Get().SetLabel("versionlabel", StrFormat("v%d.%d", MAJOR_VERSION, MINOR_VERSION));