I wonder if there is a way to get your own app version in code after you put it in the "Summary" tab in xCode. One way seems to be to search Info.plist
for CFBundleVersion
key, but is there any other, easier, more convenient way?
Asked
Active
Viewed 3.7k times
42

Rad'Val
- 8,895
- 9
- 62
- 92
-
possible duplicate of [How can my iphone app detect its own version number?](http://stackoverflow.com/questions/458632/how-can-my-iphone-app-detect-its-own-version-number) – Brad Larson Nov 01 '11 at 14:45
4 Answers
118
You can find it in the main bundle. I think it's something like:
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

Chris Ledet
- 11,458
- 7
- 39
- 47
-
Yeah, I've just found a blog(http://pigtailsoft.com/blog/?p=35) that states that. Thanks anyway! Will accept answer in 6 mins:) – Rad'Val Oct 31 '11 at 18:18
-
12Thank you, but `@"CFBundleVersion"` return the build not the version. You should get this object: `@"CFBundleShortVersionString"` – IgniteCoders Mar 08 '14 at 15:31
28
I typically use the Build number (CFBundleVersion
) to keep track of, well, the number of builds, and the Version number (CFBundleShortVersionString
) for marketing purposes. I use a run script to auto-increment my build number and I update the version number manually before each new release. So if you want to include the actual Version number in your code as opposed to the Build number, use this:
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
or
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
Here's the run script that increments the build number for anyone interested:
#!/bin/bash
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}"

lobianco
- 6,226
- 2
- 33
- 48
12
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

Aravindhan
- 15,608
- 10
- 56
- 71