iosdev

How to: update project build version automatically, in Xcode 4.3

This is very useful if you use service like TestFlight (if you are not, you should) and in general gives you idea how much you worked and tested.

Hey look, I did 500+ builds for the new version. :)

Anyway, first make sure your project’s Info.plist has Bundle version (raw key name is CFBuildVersion) set to integer value, 0 or 1 or whatever you want as starting value. In Xcode 4.3 this can be set in Summary tab, the Build field, like this:

Version is what you will market to end users, what you will respond when setting up an update in iTunes Connect. Build is mostly for developers and testers, as a reference for bug tracking/fixing.

Next, add Run Script phase in the Build Phases for the main target and paste this script:

    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}"

It looks like this in Xcode 4.3

Most important thing here is that this Run Script phase must be before Copy bundle resources. If you forget that, your builds will always be one step behind.

In the end, if you want to show this in your app, this is one way to do it:

    [NSString stringWithFormat:@"%@ (%@)",
     [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"],
     [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]
    ]