Read version from Info.plist

吃可爱长大的小学妹 提交于 2019-12-20 08:06:00

问题


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


回答1:


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



回答2:


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



回答3:


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



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



回答5:


Swift 3:

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


来源:https://stackoverflow.com/questions/4059101/read-version-from-info-plist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!