Luke Ryan Jernejcic

How To Auto-Increment Your iOS Version Number

May 03, 2012

Previously I wrote up how to auto-increment your build number. I mentioned how I wanted to figure out how to do the same for the version number. The thing that makes the version number different and more difficult is that version numbers generally have sub-versions. My bash scripting skills are minimal so I did not take on the task at the time and planned to revisit it.

Well Paul Capestany picked it up where I left off. You can see his solution on github. To understand how to install and use this script, please see the previous post for instructions.

I like Paul’s solution, but one thing that does not appeal to me is that the build number is used for the last piece of the version number, i.e. 2.4.X. The way I see things, the build number is a continuously incrementing number that should never be reset. So I went on and edited the script so that the last section increment by one every time, just like the build number. The intention behind this is that it will be reset (manually of course) when you do major version updates.

Here is the script that I ended up with (you can find the github gist here):

# Increment the build number with every build.
BUILDNUM=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILDNUM" "${PROJECT_DIR}/${INFOPLIST_FILE}"

# this splits a two-decimal version string, such as "0.45.123", allowing us to increment the third position.
VERSIONNUM=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${PROJECT_DIR}/${INFOPLIST_FILE}")
NEWSUBVERSION=`echo $VERSIONNUM | awk -F "." '{print $3}'`
NEWVERSIONSTRING=`echo $VERSIONNUM | awk -F "." '{print $1 "." $2 ".'$NEWSUBVERSION'" }'`
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $NEWVERSIONSTRING" "${PROJECT_DIR}/${INFOPLIST_FILE}"

I ended up leaving the incrementing of the build numbers in there, as I generally use both and I figure pretty much everybody else will as well.

One thing that may come up is that this only works for the last section of the version number. I figure you always want to update this piece. Unfortunately you cannot do the same with the first two sections. The computer really cannot tell when you are releasing a “significant” build, so as mentioned previously it is best to update those two manually when you make major revisions to your app.

Thanks again to Paul Capestany for commenting on the previous post with his solution. It was immensely helpful in coming up with this.

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