我正在写一个iPhone应用程序。它已经发布,但我想添加一个功能,其版本号显示。

我不希望对我发布的每个版本都手动执行此操作……

objective-C中有办法找出我的应用的版本吗?


当前回答

您可以在plist.info中指定CFBundleShortVersionString字符串,并使用提供的API以编程方式读取该字符串。

其他回答

阅读信息。你的应用程序的plist文件,并获得值的关键CFBundleShortVersionString。阅读信息。plist会给你一个NSDictionary对象

Swift版本分别为:

斯威夫特3

let versionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String

斯威夫特2

let versionNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
let buildNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") as! String

它包括在这个回购,检查一下:

https://github.com/goktugyil/EZSwiftExtensions

// Syncs with App Store and Xcode Project Settings Input
NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

您可以在plist.info中指定CFBundleShortVersionString字符串,并使用提供的API以编程方式读取该字符串。

获取X.Y.Z格式版本字符串的简单方法是:

[NSBundle mainBundle].infoDictionary[@"CFBundleVersion"]

或者,对于X.Y:

[NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"]

这两个代码段都返回字符串,您可以将其分配给标签对象的文本属性,例如。

myLabel.text = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];