Xcode - Build number from SVN

All programs come with a version number, generally composed of a number for the major version, another one for the minor version, and a last one for the maintenance version.

It may be also useful to add another number, called the «build» version, identifying more precisely the software's version.
Such a number is often represented in a hexadecimal.

You may have for instance: 1.7.5 (12B43)

The build number is especially useful when dealing with betas, where to software's version number doesn't change.

If you use a revision control system, such as Subversion (SVN) or GIT, you want to bind the build number to the revision number.

So here's a quick tutorial for the Xcode users, using Subversion.
We are going to automatically update an entry of the «Info.plist» file, with the SVN revision number.

In your application's «Info.plist» file, add an entry called «BuildNumber», and define it as a number.

Then, on your target's build phases, add a new «Run Script» phase, and place the following content:

svn update
rev=$(svn info | grep '^Revision:' | sed -e 's/^Revision: //')
/usr/libexec/PlistBuddy -c "Set :BuildNumber $rev" "$plist"

That's all. Simply replace the "MyApp/Info.plist" string with the actual path of your «Info.plist» file.

Each time you build your application, the script will update the entry in the «Info.plist» file with the latest revision number from your SVN repository.

To access the build number in Objective-C, you can use:

[ [ NSBundle mainBundle ] objectForInfoDictionaryKey: @"BuildNumber" ];

You can the build a full version string using:

[ NSString
    stringWithFormat: @"%@ (%@)",
    [ [ NSBundle mainBundle ] objectForInfoDictionaryKey: @"CFBundleVersion" ],
    [ [ NSBundle mainBundle ] objectForInfoDictionaryKey: @"BuildNumber" ]


02/24/2012 22:01
Very handy. Thanks!
Alex Bullitt
03/16/2012 18:44
You can pull a bunch of this info automatically using the build setting INFOPLIST_PREFIX_HEADER

rev=$(svn info | grep '^Last Changed Rev:' | sed -e 's/^Last Changed Rev: //')
/usr/libexec/PlistBuddy -c "Set :BuildNumber $rev" "$plist"

Also, the Last Changed Rev makes more sense to use.