【发布时间】:2010-10-30 14:25:12
【问题描述】:
我想将 Info.plist 中的包版本信息读入我的代码,最好是作为字符串。我该怎么做?
【问题讨论】:
标签: iphone objective-c cocoa-touch xcode info.plist
我想将 Info.plist 中的包版本信息读入我的代码,最好是作为字符串。我该怎么做?
【问题讨论】:
标签: iphone objective-c cocoa-touch xcode info.plist
你可以把你的 Info.plist 当作字典来阅读
[[NSBundle mainBundle] infoDictionary]
您可以通过这种方式轻松地通过CFBundleVersion 键获取版本。
最后,你可以得到版本
NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSString* version = [infoDict objectForKey:@"CFBundleVersion"];
【讨论】:
objectForInfoDictionaryKey:,因为如果可用,它会返回本地化值:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]
CFBundleVersion 已改用于 Build 版本,版本为 CFBundleShortVersionString。
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];NSString *build = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];self.versionLabel.text = [NSString stringWithFormat:@"%@.%@", version, build];
对于 Swift 用户:
if let version = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") {
print("version is : \(version)")
}
对于 Swift3 用户:
if let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") {
print("version is : \(version)")
}
【讨论】:
我知道距离任务和答案已经过去了一段时间。
由于 iOS8,接受的答案可能不起作用。
这是现在的新方法:
NSString *version = (__bridge id)CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey);
【讨论】:
现在在 iOS 8 中,这两个字段都是必需的。之前它可以在没有CFBundleShortVersionString 的情况下工作。但现在在应用商店中提交任何应用都是必填的 plist 字段。并且kCFBundleVersionKey 用于上传每个新版本,必须按递增顺序进行。专门用于 TestFlight 构建。我是这样做的,
NSString * version = nil;
version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
if (!version) {
version = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
}
【讨论】:
斯威夫特 3:
let appBuildNumber = Bundle.main.infoDictionary!["CFBundleVersion"] as! String
let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
【讨论】: