如何在iOS应用程序中以编程方式显示目标的版本/内部版本号?

问题描述 投票:128回答:4

如何以编程方式获取target version的值,如下图所示?

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

iphone xcode ipad ios-simulator
4个回答
373
投票

有两个号码!

营销版本号是针对客户的,称为版本号。它从1.0开始,主要更新到2.0,3.0,对1.1,1.2的小更新以及1.0.1,1.0.2的错误修复。此编号面向版本和新功能。它不必停在9,1.11.23是一个合理的版本号。

构建号主要是在此之前构建的内部构建数。但有些使用其他数字,如存储库的分支号或其提交号。此数字应该是唯一的,以区分不同的构建,这些构建只有少量的增量更改。


To get the version number:

Objective-C的:

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

斯威夫特<3.0:

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

Swift 3.0+(用5.0测试):

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

To get the build number:

Objective-C的:

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

斯威夫特<3.0:

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

Swift 3.0+(测试到5.0):

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

If you want both in one:

首先使用上面的行,然后使用下面的行。

Objective-C的:

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

Swift(测试到5.0):

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

Notes:

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

这是使用Xcode版本7.3(7D175)测试的。内部版本号通常用括号/括号括起来。内部版本号为十六进制或十进制。


在Xcode中,您可以通过在项目设置的Run script构建阶段中放置以下内容,将构建号自动递增为十进制数字

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

对于十六进制内部版本号,请使用此脚本

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,请执行以下操作:

Step 1

Step 2

Step 3


11
投票

您无需更改项目或Xcode中的任何内容。以下是单独的Swift版本:

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

它包含在此回购中,请查看:

https://github.com/goktugyil/EZSwiftExtensions


5
投票

这里是Swift 3的相同代码:

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

4
投票

以编程方式显示版本和内部版本号 - Swift 4.0

let versionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "1.0"

let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "1.0"            

let versionAndBuildNumber = "Ver #\(versionNumber) ( Build #\(buildNumber) )"
© www.soinside.com 2019 - 2024. All rights reserved.