This is a post expanding on the process(es) described in the accepted answer on this Stack Overflow question.
The goal here is to automate a "Build Number" in XCode where it is in the format
- 1 is the major version number
- 2 is the minor version number
- 3 is the revision/maintenance release number
- 4 is the number of builds that have been required to get to this point
The reason my script (below) differs from the proposed answer is that I wanted to be responsible for updating the "Version", which would then automatically generate/feed the "Build number".
Steps to automate the build number
1. Follow the steps from the Stack Overflow thread
In Xcode 4.2 - 5.0:
- Load your Xcode project.
- In the left hand pane, click on your project at the very top of the hierarchy. This will load the project settings editor.
- On the left-hand side of the center window pane, click on your app under the TARGETS heading. You will need to configure this setup for each project target.
- Select the Build Phases tab.
- In Xcode 4, at the bottom right, click the Add Build Phase button and select Add Run Script.
- In Xcode 5, select Editor menu > Add Build Phase > Add Run Script Build Phase.
- Drag-and-drop the new Run Script phase to move it to just before the Copy Bundle Resources phase (when the app-info.plist file will be bundled with your app).
- In the new Run Script phase, set Shell: /bin/bash.
- Copy and paste the following into the script area for integer build numbers:
2. Replace the suggested script
Instead of using the script suggested by the Stack Overflow answer, use this script:
echo "Updating Build" buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE") version=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$INFOPLIST_FILE") echo "Current Version: $version" echo "Latest Build: $buildNumber" major=$(echo $version | cut -d'.' -f 1) minor=$(echo $version | cut -d'.' -f 2) revision=$(echo $version | cut -d'.' -f 3) build=$(echo $buildNumber | cut -d'.' -f 4) echo "Major: $major" echo "Minor: $minor" echo "Revision: $revision" echo "build: $build" build=$(($build + 1)) echo "This build: $build" /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $major.$minor.$revision.$build" "$INFOPLIST_FILE"
That should be it! Enjoy.