Xcode includes something rather magical called the Apple Generic Versioning Tool. With this tool you can increase your Build Number automatically every time you hit “Build and Run” in Xcode.
Here’s how you can add it to your project. I’m using Xcode 5.1 here with a new Single View iOS project. But this will work just fine in other versions of Xcode.
By default, Version and Build are set to 1.0 (in your app target, under the General tab). We want to leave Version alone and only increase Build.
Head over to the Build Settings tab and find the “Versioning” section. It’s easiest to just search for it. In here, set the Current Project Version field to your initial build – let’s say 1 for this example.
Now select the Build Phases tab. Click on the little plus icon in the top left corner and select New Run Script Build Phase. This will allow us to execute a small script every time we build.
Open the little triangle next to Run Script and underneath the Shell field, add this bit of code. You can even add multiple lines, it’s like a little text editor.
"${DEVELOPER_BIN_DIR}/agvtool" next-version -all
Here’s what it should look like:
And that’s it! Hit CMD+B a few times and head back to the General tab to see your Build Number magically increased. You can now retrieve this value from you main Bundle (explained in my previous article).
Kudos to nekno who was kind enough to share this knowledge on Stackoverflow:
As an avid Testflight user, the build number is something I always forget to set. Thanks to you that problem is no more 🙂
Glad to hear it helped you, Isuru 😉