Version management in XCode

up:: iOS Development

Related:

Links:

Pre-release versions

I find it challenging that XCode doesn’t support pre-release versions. Here is a strategy:

  • Each production build is a multiple of 100
  • Each TestFlight build is >100

Example

  1. Release 1.0.0 (100) to Prod and Beta
  2. Notice a bugfix, and ship to Beta: 1.0.0 (101)
  3. Implement a feature on Beta: 1.0.0 (110)
  4. Package up changes on Beta. Release 1.1.0 (100) to Prod and Beta.

Automation

  • In Xcode, open your project, then select your target.
  • Go to the Build Phases tab.
  • Click the + button in the top-left and select New Run Script Phase.
  • In the new script section, paste the following code:
./increment_build_number.sh
  • Make sure this script runs before the “Compile Sources” phase by dragging it above in the Build Phases list.
# version.txt
1.0.0
#!/bin/bash
 
# Path to version.txt file
VERSION_FILE="version.txt"
 
# Get the current version from Info.plist (CFBundleShortVersionString)
CURRENT_VERSION=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}")
 
# Get the last version from version.txt
LAST_VERSION=$(cat "$VERSION_FILE")
 
# Get the current build number from Info.plist (CFBundleVersion)
CURRENT_BUILD=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}")
 
# Compare versions (ignoring build numbers)
if [ "$CURRENT_VERSION" != "$LAST_VERSION" ]; then
  # If the version has changed (e.g., 1.1.0 -> 1.2.0), reset the build number to 100
  NEW_BUILD=100
 
  # Update the version.txt file with the new version
  echo "$CURRENT_VERSION" > "$VERSION_FILE"
else
  # If the version is the same, increment the build number
  NEW_BUILD=$((CURRENT_BUILD + 1))
fi
 
# Update Info.plist with the new build number
/usr/libexec/PlistBuddy -c "Set CFBundleVersion $NEW_BUILD" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
 
echo "Updated build number to $NEW_BUILD for version $CURRENT_VERSION"