我想将Info.plist中的捆绑软件版本信息读入我的代码中,最好以字符串的形式。我怎样才能做到这一点?
Answers:
您可以将Info.plist作为字典阅读,
[[NSBundle mainBundle] infoDictionary]
这样一来,您就可以轻松获取CFBundleVersion
关键版本。
最后,您可以使用
NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSString* version = [infoDict objectForKey:@"CFBundleVersion"];
objectForInfoDictionaryKey:
它,因为它会返回本地化的值(如果可用):[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]
CFBundleVersion
已重新定位为Build和Version为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)")
}
现在,在iOS 8中,两个字段都是必需的。较早时,它不带CFBundleShortVersionString
。但是现在这是必填的plist字段,用于在应用商店中提交任何应用。并且kCFBundleVersionKey
会比较上传的每个新版本(必须按增量顺序)。专门用于TestFlight构建。我是这样的
NSString * version = nil;
version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
if (!version) {
version = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
}