Read version from Info.plist

IphoneObjective CCocoa TouchXcodeinfo.plist

Iphone Problem Overview


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

Iphone Solutions


Solution 1 - Iphone

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"];

Solution 2 - Iphone

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)")
}

Solution 3 - Iphone

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);

Solution 4 - Iphone

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];
	}

Solution 5 - Iphone

Swift 3:

let appBuildNumber = Bundle.main.infoDictionary!["CFBundleVersion"] as! String
let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn SmithView Question on Stackoverflow
Solution 1 - IphonegcampView Answer on Stackoverflow
Solution 2 - IphoneayalcinkayaView Answer on Stackoverflow
Solution 3 - IphoneMichael KesslerView Answer on Stackoverflow
Solution 4 - IphonekarimView Answer on Stackoverflow
Solution 5 - IphoneNitin NainView Answer on Stackoverflow