Read version from Info.plist
Asked Answered
B

5

94

I want to read the bundle version info from Info.plist into my code, preferably as a string. How can I do this?

Baritone answered 30/10, 2010 at 14:25 Comment(0)
R
206

You can read your Info.plist as a dictionary with

[[NSBundle mainBundle] infoDictionary]

And you can easily get the version at the CFBundleVersion key that way.

Finally, you can get the version with

NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSString* version = [infoDict objectForKey:@"CFBundleVersion"];
Retentive answered 30/10, 2010 at 14:28 Comment(6)
Perfect. That's a lot simpler than all the lines of CF* functions I was using... I was sure there must have been a quick way to get the NSDictionary.Victualage
For the version number, this doesn't matter, but for other keys you might want to use objectForInfoDictionaryKey: instead, since it returns the localized value if available: [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]Anthem
Note in Xcode4 if you look at the target's summary panel you'll see Version & Build fields. CFBundleVersion has been repurposed to be Build and Version is CFBundleShortVersionString.Laurentia
This will fetch both version & build number, and format them together: NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; NSString *build = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]; self.versionLabel.text = [NSString stringWithFormat:@"%@.%@", version, build];Driggers
use CFBundleShortVersionString as key for objectForKey method.Caseate
I think also for version number objectForInfoDictionaryKey: is desirable, because what's called "Product version" (CFBundleShortVersionString) can contain text and be localized.Lynettelynn
C
13

for Swift users:

if let version = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") {
    print("version is : \(version)")
}

for Swift3 users:

if let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") {
    print("version is : \(version)")
}
Caseate answered 24/10, 2015 at 18:3 Comment(0)
W
5

I know that some time has passed since the quest and the answer.

Since iOS8 the accepted answer might not work.

This is the new way to do it now:

NSString *version = (__bridge id)CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey);
Wedding answered 25/6, 2014 at 11:9 Comment(1)
Do you have a link to why this is so?Sansculotte
F
4

Now in iOS 8 both fields are necessary. Earlier it works without the CFBundleShortVersionString. But now it is a required plist field to submit any app in app store. And kCFBundleVersionKey is compared for uploading every new build, which must be in incremental order. Specially for TestFlight builds. I do it this way,

NSString * version = nil;
    version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    if (!version) {
        version = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
    }
Foreground answered 21/10, 2014 at 11:30 Comment(1)
Thanks .. version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; worked for meAlita
K
3

Swift 3:

let appBuildNumber = Bundle.main.infoDictionary!["CFBundleVersion"] as! String
let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
Knitter answered 30/6, 2017 at 3:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.