Integrating git version info in iOS/Cocoa apps

This is a quick reminder on how to add version info from git to your Xcode application – iOS or Cocoa – so you can see in the actual application which repository state this binary was built from.

There’s nothing new in this post really – others have done the same and blogged about it – but it serves as a note to self on how to quickly go about it and, as such, may be helpful to others. It’s really a quite simple two step process:

  1. Add a script build phase to your build target (at the end, after the other build steps):

    git status
    version=$(git describe --always --tags --dirty)
    echo version: $version
    info_plist=$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app/Info.plist
    /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $version" $info_plist

    The extra command git status to refresh the index was added, because sometimes the repos would be reported as dirty otherwise. Not sure why exactly it’s happening but this is the fix. Maybe it’s some temporary build file.

  2. Add a UILabel to one of your views (or simply NSLog to the console) and show the version:

    - (void)viewDidLoad {
      [super viewDidLoad];
      NSString *version = [[[NSBundle mainBundle] infoDictionary]
      objectForKey:@"CFBundleVersion"];
      self.versionLabel.text = version;
    }