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

时间:2021-04-08 07:26:19

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中有没有办法找出我应用的版本?

14 个解决方案

#1


216  

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)".

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

#2


143  

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.

现在手动启动一个小版本会很麻烦,所以使用源代码库修订号技巧是最理想的。如果您没有将其绑定(我也没有),上面的代码片段可能会很有用。它还会显示应用程序的显示名。

#3


46  

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:

它包含在这个回购协议中,检查一下:

https://github.com/goktugyil/EZSwiftExtensions

https://github.com/goktugyil/EZSwiftExtensions

#4


41  

This is what I did in my application

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

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

Hopefully this simple answer will help somebody...

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

#5


31  

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

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

#6


30  

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"];
    

#7


17  

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

#8


16  

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

在X.Y.中获取版本字符串的一种简洁的方法Z格式是:

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

Or, for just X.Y:

或者,只是X.Y:

[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"];

#9


7  

You can try using dictionary as:-

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

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

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

#10


4  

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

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

#11


1  

If you need a combination of both version and build num, here's a short way using Swift 3:

如果您需要两个版本和构建num的组合,请使用Swift 3:

let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"]!
let buildNum = Bundle.main.infoDictionary!["CFBundleVersion"]!
let versionInfo = "\(appVersion) (build \(buildNum))"
// versionInfo is now something like "2.3.0 (build 17)"

Add an as! String to the end of either the appVersion or buildNum line to get only that portion as a String object. No need for that though if you're looking for the full versionInfo.

添加一个!字符串到appVersion或buildNum行的末尾,以获取作为字符串对象的那一部分。如果您正在寻找完整的versionInfo,则不需要这样做。

I hope this helps!

我希望这可以帮助!

#12


0  

This is a good thing to handle with a revision control system. That way when you get a bug report from a user, you can check out that revision of code and (hopefully) reproduce the bug running the exact same code as the user.

这是一个很好的处理修改控制系统。这样,当您从用户那里获得错误报告时,您就可以检查代码的修改,(希望如此)重现与用户运行相同代码的错误。

The idea is that every time you do a build, you will run a script that gets the current revision number of your code and updates a file within your project (usually with some form of token replacement). You can then write an error handling routine that always includes the revision number in the error output, or you can display it on an "About" page.

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

#13


0  

You can use the infoDictionary which gets the version details from info.plist of you app. This code works for swift 3. Just call this method and display the version in any preferred UI element.

您可以使用infoDictionary从info获取版本细节。这段代码适用于swift 3。只需调用此方法,并在任何首选UI元素中显示版本。

Swift-3  

func getVersion() -> String {
    let dictionary = Bundle.main.infoDictionary!
    let version = dictionary["CFBundleShortVersionString"] as! String
    let build = dictionary["CFBundleVersion"] as! String
    return "v\(version).\(build)"
}

#14


0  

You can try this method:

你可以试试这个方法:

NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

#1


216  

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)".

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

#2


143  

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.

现在手动启动一个小版本会很麻烦,所以使用源代码库修订号技巧是最理想的。如果您没有将其绑定(我也没有),上面的代码片段可能会很有用。它还会显示应用程序的显示名。

#3


46  

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:

它包含在这个回购协议中,检查一下:

https://github.com/goktugyil/EZSwiftExtensions

https://github.com/goktugyil/EZSwiftExtensions

#4


41  

This is what I did in my application

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

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

Hopefully this simple answer will help somebody...

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

#5


31  

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

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

#6


30  

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"];
    

#7


17  

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

#8


16  

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

在X.Y.中获取版本字符串的一种简洁的方法Z格式是:

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

Or, for just X.Y:

或者,只是X.Y:

[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"];

#9


7  

You can try using dictionary as:-

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

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

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

#10


4  

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

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

#11


1  

If you need a combination of both version and build num, here's a short way using Swift 3:

如果您需要两个版本和构建num的组合,请使用Swift 3:

let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"]!
let buildNum = Bundle.main.infoDictionary!["CFBundleVersion"]!
let versionInfo = "\(appVersion) (build \(buildNum))"
// versionInfo is now something like "2.3.0 (build 17)"

Add an as! String to the end of either the appVersion or buildNum line to get only that portion as a String object. No need for that though if you're looking for the full versionInfo.

添加一个!字符串到appVersion或buildNum行的末尾,以获取作为字符串对象的那一部分。如果您正在寻找完整的versionInfo,则不需要这样做。

I hope this helps!

我希望这可以帮助!

#12


0  

This is a good thing to handle with a revision control system. That way when you get a bug report from a user, you can check out that revision of code and (hopefully) reproduce the bug running the exact same code as the user.

这是一个很好的处理修改控制系统。这样,当您从用户那里获得错误报告时,您就可以检查代码的修改,(希望如此)重现与用户运行相同代码的错误。

The idea is that every time you do a build, you will run a script that gets the current revision number of your code and updates a file within your project (usually with some form of token replacement). You can then write an error handling routine that always includes the revision number in the error output, or you can display it on an "About" page.

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

#13


0  

You can use the infoDictionary which gets the version details from info.plist of you app. This code works for swift 3. Just call this method and display the version in any preferred UI element.

您可以使用infoDictionary从info获取版本细节。这段代码适用于swift 3。只需调用此方法,并在任何首选UI元素中显示版本。

Swift-3  

func getVersion() -> String {
    let dictionary = Bundle.main.infoDictionary!
    let version = dictionary["CFBundleShortVersionString"] as! String
    let build = dictionary["CFBundleVersion"] as! String
    return "v\(version).\(build)"
}

#14


0  

You can try this method:

你可以试试这个方法:

NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];