Refer to build number or version number in code

NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *appVersion = [infoDict objectForKey:@"CFBundleShortVersionString"]; // example: 1.0.0
NSString *buildNumber = [infoDict objectForKey:@"CFBundleVersion"]; // example: 42 

Sure, you can ask your main bundle for the info directory which includes all keys and values of your info.plist, including the version number:

NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
NSString *version = [info objectForKey:@"CFBundleShortVersionString"];

NSBundle *bundle = [NSBundle mainBundle];   
NSString *appVersion = [bundle objectForInfoDictionaryKey:(NSString *)@"CFBundleShortVersionString"];
NSString *appBuildNumber = [bundle objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];

@robo I did follow a write-up on how to autoincrement the build number (not the version number, but that's very similar) but now I can't find it. I'll try to explain what I did. I hope it's clear enough to follow.

In Xcode 4 look in the Navigator pane, which is the pane on the left side of the Xcode screen. It has 7 icons across the top. Select the left-most icon (looks like a file folder) to get the Project Navigator pane.

Now click on the first item in that pane, which is the project itself. That opens a big window in the center with a narrow column to the left of it.

The narrow column has 2 sections: "PROJECT" AND "TARGETS". Click on your project name in the "TARGETS" section. Now the center pane has 5 tabs along the top: "Summary", "Info", "Build Settings", "Build Phases", and "Build Rules".

Click on the "Build Phases" tab. If your config is the same as mine you'll now see 4 sections in the main window: "Target Dependencies", "Copy Bundle Resources", "Compile Sources", and "Link...". Click on "Add Build Phase" (at the lower right corner) and choose "Add Run Script". A 5th section now appears in the center pane with the title "Run Script". Drag the "Run Script" section up so it is the 2nd section from the top, just below "Target Dependencies".

Now click on the disclosure triangle for the Run Script section. That opens a window where you can put a script. Copy and paste the following script into that window.

#!/bin/bash    
bN=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
bN=$((0x$bN)) 
bN=$(($bN + 1)) 
bN=$(printf "%X" $bN)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $bN" "$INFOPLIST_FILE"

Be sure that "Run script only when installing" is NOT CHECKED.

That's it! Now your build number auto-increments every time you build your project.

This script assumes you have a plain hex build number in your info.plist file and increments it as hex. I'm not sure what it does if the build number is missing. But it's easy to enter a starting value -- just go to the "Summary" tab in the same center pane you were using above and enter "1" (or whatever value you like) into the "Build" box (but not a formatted string like 1.0).


I think the hex script I gave first is not valid for submission to the App Store. Here is the same script using decimal arithmetic.

#!/bin/bash    
bN=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE") 
((bN += 1))
bN=$(printf "%d" $bN)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $bN" "$INFOPLIST_FILE"

You can get info.plist values with this piece of code (replace CFBundleShortVersionString with the info.plist's key you want cf. Recommended Info.plist Keys)

[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]