Auto-Incrementing Build Numbers in XCode

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
  • CFBundleShareVersion
  • CFBundleLongVersion

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

Script below:

# This was taken from variations from:

# 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"];

3 thoughts on “Auto-Incrementing Build Numbers in XCode

  1. For the bulleted list of fields:
    CFBundleShareVersion should be CFBundleShortVersionString
    CFBundleLongVersion should be CFBundleLongVersionString

  2. Nitesh, go to your project file, then choose your target, then “Build Phases”, there’s a little + button at the bottom that says “Add Build Phase”. Select that and choose Add Run Script.

Leave a Reply

Your email address will not be published. Required fields are marked *