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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/458632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:33:45  来源:igfitidea点击:

How can my iphone app detect its own version number?

iphoneiosversion

提问by

I'm writing an iPhone app. It's already been published, but I would like to add a feature where its version number is displayed.

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

I'd rather not have to do this manually with each version I release...

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

Is there a way in objective-C to find out what the version is of my app?

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

回答by Brad Larson

As I describe here, I use a script to rewrite a header file with my current Subversion revision number. That revision number is stored in the kRevisionNumber constant. I can then access the version and revision number using something similar to the following:

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

[NSString stringWithFormat:@"Version %@ (%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"], kRevisionNumber]

which will create a string of the format "Version 1.0 (51)".

这将创建一个格式为“Version 1.0 (51)”的字符串。

回答by idStar

Building on Brad Larson's answer, if you have major and minor version info stored in the info plist (as I did on a particular project), this worked well for me:

基于 Brad Larson 的回答,如果您在 info 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];
}

Now revving a minor version manually can be a pain, and so using a source repository revision number trick is ideal. If you've not tied that in (as I hadn't), the above snippet can be useful. It also pulls out the app's display name.

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

回答by Esqarrouth

Swift version for both separately:

Swift 版本分别用于:

Swift 3

斯威夫特 3

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


Swift 2

斯威夫特 2

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

Its included in this repo, check it out:

它包含在此 repo 中,请查看:

https://github.com/goktugyil/EZSwiftExtensions

https://github.com/goktugyil/EZSwiftExtensions

回答by Arkady

This is what I did in my application

这就是我在我的应用程序中所做的

NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

Hopefully this simple answer will help somebody...

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

回答by Jasper Bekkers

You can specify the CFBundleShortVersionStringstring in your plist.info and read that programmatically using the provided API.

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

回答by Shaik Riyaz

There are two things - build version and app version.

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

  1. To get App version:

    NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    
  2. To get Build version:

    NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    
  1. 获取应用版本:

    NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    
  2. 获取构建版本:

    NSString *buildVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    

回答by John Erck

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

回答by rodamn

A succinct way to obtain a version string in X.Y.Zformat is:

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

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

Or, for just X.Y:

或者,仅针对XY:

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

Both of these snippets returns strings that you would assign to your label object's text property, e.g.

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

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

回答by Abhishek Verma

You can try using dictionary as:-

您可以尝试使用字典作为:-

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

NSString *buildVersion = infoDictionary[(NSString*)kCFBundleVersionKey];
NSString *bundleName = infoDictionary[(NSString *)kCFBundleNameKey]

回答by lostInTransit

Read the info.plist file of your app and get the value for key CFBundleShortVersionString. Reading info.plist will give you an NSDictionary object

阅读您的应用程序的 info.plist 文件并获取密钥 CFBundleShortVersionString 的值。阅读 info.plist 会给你一个 NSDictionary 对象