Build Numbers in Xcode

The “Build Number”, also known as “Bundle Version” is a field of your info.plist file in your app project. This help helps you build and sort different builds of your app.

This field is pretty much free from. You can set your build numbers as you want. I used to use a YYMMDD format – Before releasing an app, I would set this field manually and then submit it to iTunes. This is why Mignori has funny bundle numbers: The first version was something like 131229, indicating I completed it and submitted it to iTunes on December 29, 2013. The third build number was 140309, indicating I submitted that version on April 9, 2014, and so on. What I like about doing things like this is that you know exactly when you finished a version and uploaded it to iTunes.

But, more often than not, the build number is either the number of times you built your project – That is, the number of times you clicked the “Play” button on Xcode, or the number of Commits on your Git repo. I eventually started doing the Git repo way, because sometimes I would forget to update the build number manually and it would be a little bit annoying to fix.

Specially with WatchKit apps now. Things are slightly more complicated, but nothing too extreme. Your main app’s Build Number and your WatchKit app’s Build Number have to match. If they don’t watch, Xcode will refuse to compile your project. So save yourself a little bit of trouble it’s easier to have this field update automatically instead of setting it twice.

You can use this bash script to set the build numbers to the number of commits on your git repo:

# Set the build number to the count of Git commits
buildNumber=$(git rev-list --count HEAD)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"

I graciously got this snippet from StackOverflow. But it was a long time ago, so unfortunately I cannot properly credit the author. If you know the author of this script, please let me know so I can update this post. Thanks!

To automatically update your build numbers to the number of git commits in your repo, add the script as following:

  1. Click on your project’s “blue print”.
  2. Select your main app target.
  3. Select “Build Phases” on the tab at the top.
  4. Click the + button, and select “New Run Script Phase”
  5. Copy and paste the script there.
  6. Repeat steps 5 – 6 for your WatchKit app (Select your WatchKit target first).

Xcode Build Numbers

(Click to enlarge).

Now, whenever you click the “Play” button on Xcode, the Build Number will be updated automatically.

If you prefer, you can adapt this to make your Build Number the number of times you physically built your app. The gist is to grab the current build number, add one, and then put it in the appropriate environment variable.

Positive SSL