Sunday, September 9, 2012

AUTO INCREMENT BUILD NUMBER IN XCODE 4.2


         Going forward, if you look on the Info tab for your Application Target, you should use the "Bundle versions string, short" as your Version (e.g., 3.4.0) and "Bundle version" as your Build (e.g., 500 or 1A500). If you don't see them both, you can add them. Those will map to the proper Version and Build textboxes on the Summary tab.

There are all sorts of schemes, but a popular one is:
{MajorVersion}.{MinorVersion}.{Revision}
  • Major version - Major changes, redesigns, and functionality changes
  • Minor version - Minor improvements, additions to functionality
  • Revision - A patch number for bug-fixe

Then the Build is used separately to indicate the total number of builds for a release or for the entire product lifetime.
Many developers start the Build number at 0, and every time they build they increase the number by one, increasing forever. In my projects, I have a script that automatically increases the build number every time I build. See instructions for that below.
  • Release 1.0.0 might be build 542. It took 542 builds to get to a 1.0.0 release.
  • Release 1.0.1 might be build 578.
  • Release 1.1.0 might be build 694.
  • Release 2.0.0 might be build 949.

Auto-incrementing build numbers in Xcode

It's a pain to build project in Xcode only to find out later that you forgot to update the build number.
In Xcode 4.2:
  1. Load your Xcode project.
  2. In the left hand pane, click on your project at the very top of the hierarchy. This will load the project settings editor.
  3. 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.
  4. Select the Build Phases tab.
  5. At the bottom right, click the Add Build Phase button and select Add Run Script.
  6. Drag-and-drop the new Run Script phase to move it to just before the Copy Bundle Resourcesphase (when the app-info.plist file will be bundled with your app).
  7. In the new Run Script phase, leave the Shell: /bin/sh value alone. Copy and paste the following into the script area for integer build numbers:
# echo $(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion"${PROJECT_DIR}/${PROJECT_NAME}/${PROJECT_NAME}-Info.plist)
echo "Current Build version $buildNumber"
(( buildNumber += 1 )) #increment build number by one
echo "New Build version $buildNumber"

/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber"${PROJECT_DIR}/${PROJECT_NAME}/${PROJECT_NAME}-Info.plist


Note you can view the echo message at log navigator in xcode.

You can access the version information using the following code
--------------------------------------------------------------
NSString * version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSString * buildNo = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBuildNumber"];
NSString * buildDate = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBuildDate"];
NSLog(@"Application Version: %@ Build No: %@ Build Date: %@",version,buildNo,buildDate);

No comments:

Post a Comment