Increase the build number while Archiving in Xcode

If you are a iOS or MAC software developer, there are pretty dense chances that you usually archive the build and send it in-house (Through Crashlytics or TestFlight to distribute build to client and organization people) or to the Apple App store.

Archiving in the context of iOS and Xcode is the process where you generate an ipa file which will be installed on the user device

Frequently each time we send a build out, we have to make sure to increment a build number. (Which is usually one higher than previous shipment). However, this is painstaking and quite error prone approach to follow. In order to deal with that, I use a build increment script which automatically increments build number every time it is archived.

You can also customize it to increase build number every time you make a development build. (More about it will be explained later)

Here are steps how to set it up :

  1. Click on the project name in left side panel
  2. Go to TARGETS and select your base project
  3. Click on build phases tab on the top right side

Here's how it will look like,

Build Phase Example

Alright? Now click on + icon on the top and select New Run Script Phase and add new run script.

Part of Xcode will look like this

Run Script Phase

Do you see that black space in the picture? That is exactly the place where you would add a script which will automatically increment the build number every time you archive project.

Here goes the 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}"

Copy this entire script in the provided blank space.

Now, there are two things you might want to do based on whether you want to increment build number every time you make a developer build

Like running a project on simulator or device locally without going through tedious archiving process.

Look at the following screenshot for Run Script phase

Run script Options

  1. Check the second box which says Run script only when installing if you want this script to increment build number only while archiving project

  2. If you want to increment build number every time you run the project, keep the second box unchecked. (As shown in the image)

I really had a good experience with this script so far. Hope this helps to someone who is trying to achieve the similar result.