It's easy to set the build and version numbers manually in Xcode, and I learned how to increment them with a macro at build time. But how do I reference their values in code?
-
do you have a reference / link about how to autocreate version #s ? – phlebotinum Apr 04 '12 at 16:27
7 Answers
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *appVersion = [infoDict objectForKey:@"CFBundleShortVersionString"]; // example: 1.0.0
NSString *buildNumber = [infoDict objectForKey:@"CFBundleVersion"]; // example: 42

- 21,309
- 5
- 56
- 97
-
https://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html documents the keys that can be in an app's (well, any bundle actually) Info.plist, including these two. – Andrew Madsen Oct 02 '14 at 03:31
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"];

- 25,168
- 7
- 79
- 97
-
2Wow 5 good answers within 12 minutes of posting the question! All deserve a green check but I can only accept one so I'm taking the one with the earliest timestamp. – RobertL Apr 08 '12 at 16:24
-
@RobertL In this case, you could upvote their answers to give them some reputation. – JustSid Apr 08 '12 at 16:50
-
-
3To those who prefer brevity: `version = NSBundle.mainBundle.infoDictionary[@"CFBundleVersion"]` – mojuba Feb 27 '16 at 22:11
NSBundle *bundle = [NSBundle mainBundle];
NSString *appVersion = [bundle objectForInfoDictionaryKey:(NSString *)@"CFBundleShortVersionString"];
NSString *appBuildNumber = [bundle objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];

- 8,748
- 2
- 31
- 33
@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"

- 11,546
- 5
- 41
- 64

- 14,214
- 11
- 38
- 44
-
To my surprise I've learned that hex version numbers don't pass validating for the App Store. I'm posting a reply below with a revised version of the script that does decimal arithmetic. See below. – RobertL Jan 26 '13 at 17:41
-
I went ahead and edited in your decimal script to the bottom of this answer. That way the other answer can be removed, cutting down some on clutter. – thegrinner Jun 28 '13 at 18:33
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"]

- 2,812
- 25
- 41
Since those values are stored in your app's Info.plist
you can read then via the methods provided on NSBundle
:
NSString * version = [[NSBundle mainBundle] objectForInfoDictionaryKey: CFBundleShortVersionString];
See Accessing bundles contents as the authoritative reference on getting data from your Info.plist file and Core Foundation keys for available Info.plist keys.

- 17,918
- 1
- 43
- 70
In Swift:
let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String

- 605
- 5
- 7