如何在iOS应用程序中以编程方式显示版本/构建目标数量?

时间:2022-01-15 07:26:47

How can I programmatically get the value of the target version, like in the image below?

如何以编程方式获取目标版本的值,如下图所示?

As seen in the Properties window of the target of my Xcode project. I want to display this in the splash screen of my app so I know which version the people are currently using?

正如在我的Xcode项目目标的属性窗口中看到的。我想把它显示在我的应用程序的启动屏幕上,这样我就知道人们现在使用的是哪个版本?

3 个解决方案

#1


342  

There are 2 Numbers!

有两个数字!

The marketing release number is for the customers, called version number. It starts with 1.0 and goes up for major updates to 2.0, 3.0, for minor updates to 1.1, 1.2 and for bug fixes to 1.0.1, 1.0.2 . This number is oriented about releases and new features. It does not have to stop at 9, 1.11.23 is a reasonable version number.

市场发行版是为客户提供的,称为版本号。它从1.0开始,主要更新为2.0,3.0,次要更新为1.1,1.2,bug修复为1.0.1,1.0.2。这个数字面向发布和新特性。它不必停在9,1.11.23是一个合理的版本号。

The build number is mostly the internal number of builds that have been made until then. But some use other numbers like the branch number of the repository or its commit number. This number should be unique to distinguish the different builds, which only have minor incremental changes.

构建号主要是到那时为止所做的构建的内部数量。但是有些使用其他数字,比如存储库的分支号或提交号。这个数字应该是唯一的,以区分不同的构建,它们只有微小的增量变化。


To get the version number:

Objective-C:

objective - c:

NSString * appVersionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

Swift < 3.0:

斯威夫特< 3.0:

let appVersionString: String = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String

Swift 3.0:

斯威夫特3.0:

let appVersionString: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String

To get the build number:

Objective-C:

objective - c:

NSString * appBuildString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

Swift < 3.0:

斯威夫特< 3.0:

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

Swift 3.0:

斯威夫特3.0:

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

If you want both in one:

You then have to use the use the above lines first.

然后您必须首先使用上面的行。

Objective-C:

objective - c:

NSString * versionBuildString = [NSString stringWithFormat:@"Version: %@ (%@)", appVersionString, appBuildString];

Swift:

迅速:

let versionAndBuildNumber: String = "\(versionNumber) (\(buildNumber))"

Notes:

The values in the main bundle are not always present, for example in a command line application there is no CFBundleShortVersionString or CFBundleVersion, so the methods will return nil and it will crash because in the code it makes a incorrect downcast. But in normal Cocoa iOS and Mac apps these values are defined and will not be deleted.

主包中的值并不总是存在的,例如,在命令行应用程序中,没有CFBundleShortVersionString或CFBundleVersion,因此这些方法将返回nil,它将崩溃,因为在代码中,它进行了错误的下行。但是在普通的Cocoa iOS和Mac应用程序中,这些值是被定义的,不会被删除。

This is tested with Xcode Version 7.3 (7D175). The build number is often written in parenthesis / braces. The build number is in hexadecimal or decimal.

这是用Xcode 7.3版(7D175)测试的。构建号通常用括号/大括号来写。构建数是十六进制或十进制的。

如何在iOS应用程序中以编程方式显示版本/构建目标数量?


In Xcode you can auto-increment the build number as a decimal number by placing the following in the Run script build phase in the project settings

在Xcode中,您可以通过在项目设置的运行脚本构建阶段中放置以下内容,将构建数作为小数自动递增

#!/bin/bash    
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

For hexadecimal build number use this script

对于十六进制构建数,请使用此脚本

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$((0x$buildNumber)) 
buildNumber=$(($buildNumber + 1)) 
buildNumber=$(printf "%X" $buildNumber)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

For Xcode do the following:

对于Xcode做以下操作:

Step 1

如何在iOS应用程序中以编程方式显示版本/构建目标数量?

Step 2

如何在iOS应用程序中以编程方式显示版本/构建目标数量?

Step 3

如何在iOS应用程序中以编程方式显示版本/构建目标数量?

#2


9  

You don't need to change anything in your project or Xcode. Here's the Swift version for both separately:

您不需要更改项目或Xcode中的任何内容。以下分别是Swift版本:

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

It's included in this repo, check it out:

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

https://github.com/goktugyil/EZSwiftExtensions

https://github.com/goktugyil/EZSwiftExtensions

#3


4  

Here same code for Swift 3:

Swift码3:

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

#1


342  

There are 2 Numbers!

有两个数字!

The marketing release number is for the customers, called version number. It starts with 1.0 and goes up for major updates to 2.0, 3.0, for minor updates to 1.1, 1.2 and for bug fixes to 1.0.1, 1.0.2 . This number is oriented about releases and new features. It does not have to stop at 9, 1.11.23 is a reasonable version number.

市场发行版是为客户提供的,称为版本号。它从1.0开始,主要更新为2.0,3.0,次要更新为1.1,1.2,bug修复为1.0.1,1.0.2。这个数字面向发布和新特性。它不必停在9,1.11.23是一个合理的版本号。

The build number is mostly the internal number of builds that have been made until then. But some use other numbers like the branch number of the repository or its commit number. This number should be unique to distinguish the different builds, which only have minor incremental changes.

构建号主要是到那时为止所做的构建的内部数量。但是有些使用其他数字,比如存储库的分支号或提交号。这个数字应该是唯一的,以区分不同的构建,它们只有微小的增量变化。


To get the version number:

Objective-C:

objective - c:

NSString * appVersionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

Swift < 3.0:

斯威夫特< 3.0:

let appVersionString: String = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String

Swift 3.0:

斯威夫特3.0:

let appVersionString: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String

To get the build number:

Objective-C:

objective - c:

NSString * appBuildString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];

Swift < 3.0:

斯威夫特< 3.0:

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

Swift 3.0:

斯威夫特3.0:

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

If you want both in one:

You then have to use the use the above lines first.

然后您必须首先使用上面的行。

Objective-C:

objective - c:

NSString * versionBuildString = [NSString stringWithFormat:@"Version: %@ (%@)", appVersionString, appBuildString];

Swift:

迅速:

let versionAndBuildNumber: String = "\(versionNumber) (\(buildNumber))"

Notes:

The values in the main bundle are not always present, for example in a command line application there is no CFBundleShortVersionString or CFBundleVersion, so the methods will return nil and it will crash because in the code it makes a incorrect downcast. But in normal Cocoa iOS and Mac apps these values are defined and will not be deleted.

主包中的值并不总是存在的,例如,在命令行应用程序中,没有CFBundleShortVersionString或CFBundleVersion,因此这些方法将返回nil,它将崩溃,因为在代码中,它进行了错误的下行。但是在普通的Cocoa iOS和Mac应用程序中,这些值是被定义的,不会被删除。

This is tested with Xcode Version 7.3 (7D175). The build number is often written in parenthesis / braces. The build number is in hexadecimal or decimal.

这是用Xcode 7.3版(7D175)测试的。构建号通常用括号/大括号来写。构建数是十六进制或十进制的。

如何在iOS应用程序中以编程方式显示版本/构建目标数量?


In Xcode you can auto-increment the build number as a decimal number by placing the following in the Run script build phase in the project settings

在Xcode中,您可以通过在项目设置的运行脚本构建阶段中放置以下内容,将构建数作为小数自动递增

#!/bin/bash    
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

For hexadecimal build number use this script

对于十六进制构建数,请使用此脚本

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$((0x$buildNumber)) 
buildNumber=$(($buildNumber + 1)) 
buildNumber=$(printf "%X" $buildNumber)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

For Xcode do the following:

对于Xcode做以下操作:

Step 1

如何在iOS应用程序中以编程方式显示版本/构建目标数量?

Step 2

如何在iOS应用程序中以编程方式显示版本/构建目标数量?

Step 3

如何在iOS应用程序中以编程方式显示版本/构建目标数量?

#2


9  

You don't need to change anything in your project or Xcode. Here's the Swift version for both separately:

您不需要更改项目或Xcode中的任何内容。以下分别是Swift版本:

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

It's included in this repo, check it out:

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

https://github.com/goktugyil/EZSwiftExtensions

https://github.com/goktugyil/EZSwiftExtensions

#3


4  

Here same code for Swift 3:

Swift码3:

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