KISS

Keep It Simple Stupid

Xcode: insert git build info into iOS app

| comments

Update on 2013-08-24: If you need a solution for Android, check out this post.

Update on 2013-06-09: I’ve updated the plist_build_info.sh script so that it inserts the tag for the commit, if you’re not on a branch now.

It’s a very useful feature when the build process automatically stores the build info (e.g., date, time, git branch and commit hash), so that it can be read from your app in runtime.

It’s quite easy to do in Xcode, here’s a script for that:

(plist_build_info.sh) download
1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/sh

git_version=$(git log -1 --format="%h")
git_branch=$(git symbolic-ref --short -q HEAD)
git_tag=$(git describe --tags --exact-match 2>/dev/null)

build_time=$(date)
git_branch_or_tag="${git_branch:-${git_tag}}"

info_plist="${BUILT_PRODUCTS_DIR}/${EXECUTABLE_FOLDER_PATH}/Info.plist"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion '${git_branch_or_tag}-${git_version}'" "${info_plist}"
/usr/libexec/PlistBuddy -c "Set :BuildTime '${build_time}'" "${info_plist}"

Download it to the directory where your .xcodeproj/.xcworkspace file is. Open the project/workspace in Xcode, (optional) add the script to the Supporting Files group, but uncheck its target membership on the right:

Click the project on the left, then select your target, Build Phases tab, click the Add Build Phase button at the bottom, select Add Run Script menuitem, (recommended) rename the added step to 'Save build info into info.plist', and set the command to ${SRCROOT}/plist_build_info.sh:

Don’t forget to open the app’s plist file (YourProject-Info.plist) and add an empty row with key = BuildTime and type = String:

To get the values, use like this:

1
2
3
4
5
6
7
8
- (NSString *)appVersion {
    NSDictionary *infoDict = [NSBundle mainBundle].infoDictionary;
    NSString *version = [NSString stringWithFormat:@"%@ (%@, %@)",
                         [infoDict objectForKey:@"CFBundleShortVersionString"],
                         [infoDict objectForKey:@"CFBundleVersion"],
                         [infoDict objectForKey:@"BuildTime"]];
    return version;
}

For instance, show the build info in a view controller:

As an added benefit, in the Xcode’s Organizer you can click on Applications of your device and look at the version of the app installed:

You can try this at home! :)

Comments