Luke Ryan Jernejcic

How To Auto-Increment Your iOS Build Number

April 03, 2012

UPDATE: The scripts on this page have been updated to auto-update the version number as well and can be found here.

Things do not always go smoothly in Xcode, as I have mentioned before. Since I have had to do many builds with small tweaks and upload them to TestFlight to test my app, I started to get lazy with my build numbers. As such, I decided that it was time to figure out how to automate incrementation of it.

There is a lot on the web documenting different ways to this. Many of them seem overly complicated. The two most helpful sources for me were by Fredrik Olsson and, of course, StackOverflow. With these posts and their comments I was able to work up a solution that met my requirements:

  1. Be as simple as possible.
  2. Only increment on release and ad hoc builds.

A lot of the examples floating around are using a script that calls a script. I do not to have to track another file for something like this, and I do not want to be messing with multiple languages in the same project. I want it in Xcode, in the project settings, period. And a lot of the default examples only show how to increment but not how to control when it happens. Maybe I am picky, but I want the build number to increment on significant builds but not when I build the project seventeen times to debug something.

So here is the script that I came up with from Fredrik’s example and the comments on his post:

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"

And where to put this? I am using Xcode 4.3.2. From the Project Navigator, click on your project, then click on your target. Then click on the Build Phases. On the bottom right click on Add Build Phase, then Add Run Script. Drag the new section to right above Copy Bundle Resources. Then past the script above into it. And then check the box for Run scripts only when installing.

Seems to be working pretty well for me. Ultimately I would like to get it where it combines a build number prefixed with the version number, but I did not have the time to figure that out. If anybody else is doing it that way, leave me a comment.

Also the original script from Fredrik Olsson uses a separate variable, not the Xcode build variable. If that sounds more appealing to you, check it out.


Written by Luke Ryan Jernejcic who lives and works in Austin Texas building useful things. Follow him on Twitter