应用程序中以编程方式显示目标的版本

应用程序中以编程方式显示目标的版本

本文介绍了如何在 iOS 应用程序中以编程方式显示目标的版本/内部版本号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如我的 Xcode 项目目标的属性"窗口所示.我想在我的应用程序的启动画面中显示它,以便我知道人们当前使用的是哪个版本?

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?

推荐答案

2个数字!

营销版本号是针对客户的,称为版本号.它从 1.0 开始,主要更新到 2.03.0,对于 1.1 的次要更新,1.2 以及对 1.0.11.0.2 的错误修复.这个数字是关于发布和新功能的.它不必停在 9,1.11.23 是一个合理的版本号.

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.

内部版本号主要是内部版本号.但是有些使用其他数字,例如存储库的分支编号或其提交编号.这个数字应该唯一以区分不同的构建,这些构建只有微小的增量变化.

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.

目标-C:

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

斯威夫特<3.0:

Swift < 3.0:

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

Swift 3.0+(使用 5.0 测试):

Swift 3.0+ (tested with 5.0):

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

要获取构建编号:

目标-C:

To get the build number:

Objective-C:

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

斯威夫特<3.0:

Swift < 3.0:

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

Swift 3.0+(测试到 5.0):

Swift 3.0+ (tested until 5.0):

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

如果你想要两者合二为一:

首先使用以上几行,然后使用以下几行.

If you want both in one:

First use the above lines and then the following one.

目标-C:

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

Swift(测试到 5.0):

Swift (tested until 5.0):

let versionAndBuildNumber: String = "(appVersionString) ((buildNumber))"

注意事项:

主包中的值并不总是存在,例如在命令行应用程序中没有 CFBundleShortVersionStringCFBundleVersion,因此这些方法将返回 nil 并且它会崩溃,因为在代码中它做出了不正确的向下转换.但是在普通的 Cocoa iOS 和 Mac 应用程序中,这些值是定义的,不会被删除.

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.

这是用 Xcode 7.3 (7D175) 测试的.内部版本号通常写在括号/大括号中.内部版本号为十六进制或十进制.

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中,您可以将内部版本号自动增加为十进制数,方法是在运行脚本 项目设置中的构建阶段

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

#!/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"

对于 Xcode,请执行以下操作:

这篇关于如何在 iOS 应用程序中以编程方式显示目标的版本/内部版本号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:48