我的iphone应用程序如何检测自己的版本号?

250 iphone version ios

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

对于我发布的每个版本,我宁愿不必手动执行此操作...

在Objective-C中有没有办法找出我的应用程序的版本是什么?

Bra*_*son 219

正如我在这里描述的,我使用脚本用我当前的Subversion修订版号重写头文件.该修订号存储在kRevisionNumber常量中.然后,我可以使用类似于以下内容的方式访问版本和修订号:

[NSString stringWithFormat:@"Version %@ (%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"], kRevisionNumber]
Run Code Online (Sandbox Code Playgroud)

这将创建格式为"Version 1.0(51)"的字符串.

  • 这返回了我的构建版本所以我使用了这个.[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] (152认同)
  • CFBundleVersion不正确,因为Xcode错误地使用内部版本而不是版本号填充该plist条目.jspooner是对的. (6认同)
  • 有关版本号与内部版本号的详细说明,请参阅http://stackoverflow.com/questions/6851660/version-vs-build-in-xcode-4.确认`CFBundleShortVersionString`你通常想要'版本'和'CFBundleVersion`为Build编号. (4认同)
  • kRevisionNumber似乎对我不起作用.为了获得指定的输出,我做了这个(使用@jspooner的修复):`[NSString stringWithFormat:@"Version:%@(%@)",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"], [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]]` (3认同)

idS*_*tar 147

基于Brad Larson的答案,如果您在信息plist中存储了主要和次要版本信息(就像我在特定项目中所做的那样),这对我来说效果很好:

- (NSString *)appNameAndVersionNumberDisplayString {
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appDisplayName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    NSString *majorVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSString *minorVersion = [infoDictionary objectForKey:@"CFBundleVersion"];

    return [NSString stringWithFormat:@"%@, Version %@ (%@)", 
                appDisplayName, majorVersion, minorVersion];
}
Run Code Online (Sandbox Code Playgroud)

现在手动加载次要版本可能会很麻烦,因此使用源存储库修订号技巧是理想的.如果您没有将其绑定(因为我没有),上面的代码段可能很有用.它还会拉出应用程序的显示名称.


Esq*_*uth 57

Swift版本分别为:

斯威夫特3

let versionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String
Run Code Online (Sandbox Code Playgroud)

斯威夫特2

let versionNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
let buildNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleVersion") as! String
Run Code Online (Sandbox Code Playgroud)

它包含在这个回购中,请查看:

https://github.com/goktugyil/EZSwiftExtensions


Ark*_*ady 42

这就是我在申请中所做的

NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
Run Code Online (Sandbox Code Playgroud)

希望这个简单的答案能帮助某人......


Jas*_*ers 31

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


Sha*_*yaz 30

有两件事 - 构建版本和应用程序版本.

  1. 要获得应用版本:

    NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    
    Run Code Online (Sandbox Code Playgroud)
  2. 要获得Build版本:

    NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    
    Run Code Online (Sandbox Code Playgroud)


Joh*_*rck 17

// Syncs with App Store and Xcode Project Settings Input
NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
Run Code Online (Sandbox Code Playgroud)


rod*_*amn 16

获取XYZ格式的版本字符串的简洁方法是:

[NSBundle mainBundle].infoDictionary[@"CFBundleVersion"]
Run Code Online (Sandbox Code Playgroud)

或者,对于XY:

[NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"]
Run Code Online (Sandbox Code Playgroud)

这两个片段都返回您将分配给标签对象的文本属性的字符串,例如

myLabel.text = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];
Run Code Online (Sandbox Code Playgroud)


小智 7

您可以尝试使用字典: -

NSDictionary *infoDictionary = [[NSBundle mainBundle]infoDictionary];

NSString *buildVersion = infoDictionary[(NSString*)kCFBundleVersionKey];
NSString *bundleName = infoDictionary[(NSString *)kCFBundleNameKey]
Run Code Online (Sandbox Code Playgroud)


den*_*T30 5

斯威夫特 5:

有两件事 - 应用程序版本和构建版本

感谢@Brad Larson?了很多


Red*_*ter 0

这是使用版本控制系统处理的一件好事。这样,当您从用户那里收到错误报告时,您可以检查代码的修订版本,并(希望)重现运行与用户完全相同的代码的错误。

这个想法是,每次进行构建时,您都将运行一个脚本,该脚本获取代码的当前修订号并更新项目中的文件(通常使用某种形式的令牌替换)。然后,您可以编写一个错误处理例程,该例程始终在错误输出中包含修订号,或者可以将其显示在“关于”页面上。