Danny's Lab

Engineering the World

Auto-Incrementing Build Numbers in Xcode

Published on: Feb 5, 2011
Reading time: 1 minute

When getting feedback from your users, it's important to know what version of your software they're having problems with. Here's a simple way to have an always incrementing build number in your project.

This script was adapted from a discussion on Dave Delong's Blog. I also took the liberty of adding a date/time stamp.

It fills in these fields in your Info.plist file:

  • CFBuildVersion
  • CFBuildNumber
  • CFBuildDate
  • CFBundleVersion
  • CFBundleShortVersionString
  • CFBundleLongVersionString

In order to avoid errors in your build, you may need to first add these strings to your Info.plist.

Script below:

#!/bin/bash # This was taken from variations from: # http://davedelong.com/blog/2009/04/15/incrementing-build-numbers-xcode buildPlist="${PRODUCT_NAME}-Info.plist" # Get the existing buildVersion and buildNumber values from the buildPlist buildVersion=$(/usr/libexec/PlistBuddy -c "Print CFBuildVersion" $buildPlist) buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBuildNumber" $buildPlist) buildDate=$(date "+%Y%m%d.%H%M%S") # Increment the buildNumber buildNumber=$(($buildNumber + 1)) # Set the version numbers in the buildPlist /usr/libexec/PlistBuddy -c "Set :CFBuildNumber $buildNumber" $buildPlist /usr/libexec/PlistBuddy -c "Set :CFBuildDate $buildDate" $buildPlist /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildVersion.$buildNumber" $buildPlist /usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $buildVersion.$buildNumber" $buildPlist /usr/libexec/PlistBuddy -c "Set :CFBundleLongVersionString $buildVersion.$buildNumber.$buildDate" $buildPlist

You can retrieve this information from within your program just like anything else in your Info.plist. This example extracts the short version string:

NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; NSString *currentVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];