如何以编程方式在iOS应用中显示目标的版本/内部版本号? [英] How to programmatically display version/build number of target in iOS app?

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

问题描述

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

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

As在我的Xcode项目目标的Properties窗口中看到。我想在我的应用程序的启动画面中显示这个,以便我知道人们当前使用的是哪个版本?

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.0 3.0 的主要更新,用于 1.1 的次要更新, 1.2 以及 1.0.1 1.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.

Objective-C:

Objective-C:

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

Swift< 3.0:

Swift < 3.0:

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

Swift 3.0:

Swift 3.0:

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



获取构建号码:



Objective-C:

To get the build number:

Objective-C:

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

Swift< 3.0:

Swift < 3.0:

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

Swift 3.0:

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



注意:



主包中的值并不总是存在,例如在命令行应用程序中没有 CFBundleShortVersionString CFBundleVersion ,因此方法将返回 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应用中显示目标的版本/内部版本号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆