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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3015796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:25:53  来源:igfitidea点击:

How to programmatically display version/build number of target in iOS app?

iphonexcodeipadios-simulator

提问by slim

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

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

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 项目目标的“属性”窗口所示。我想在我的应用程序的启动画面中显示它,以便我知道人们当前使用的是哪个版本?

回答by Binarian

There are 2Numbers!

2个数字!

The marketing release number is for the customers, called version number. It starts with 1.0and goes up for major updates to 2.0, 3.0, for minor updates to 1.1, 1.2and 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.23is a reasonable version number.

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

The build numberis mostly the internal number of buildsthat 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 uniqueto distinguish the different builds, which only have minor incremental changes.

版本号是大多是内部号码的建立已取得直到那时。但是有些使用其他数字,例如存储库的分支编号或其提交编号。这个数字应该是唯一的,以区分不同的构建,只有微小的增量变化。



To get the versionnumber:

获取版本号:

Objective-C:

目标-C:

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

Swift < 3.0:

斯威夫特 < 3.0:

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

Swift 3.0+ (tested with 5.0):

Swift 3.0+(用 5.0 测试):

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

To get the buildnumber:

要获取内部版本号:

Objective-C:

目标-C:

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

Swift < 3.0:

斯威夫特 < 3.0:

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

Swift 3.0+ (tested until 5.0):

Swift 3.0+(测试到 5.0):

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

If you want bothin one:

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

First use the above lines and then the following one.

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

Objective-C:

目标-C:

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

Swift (tested until 5.0):

Swift(测试到 5.0):

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

Notes:

笔记:

The values in the main bundle are not always present, for example in a command line application there is no CFBundleShortVersionStringor CFBundleVersion, so the methods will return niland 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.

主包中的值并不总是存在,例如在命令行应用程序中没有CFBundleShortVersionStringor 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) 测试的。内部版本号通常写在括号/大括号中。内部版本号为十六进制或十进制。

buildandversion

构建和版本



In Xcodeyou can auto-increment the build numberas a decimal numberby placing the following in the Run scriptbuild phase in the project settings

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"

For hexadecimalbuild 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 Xcodedo the following:

为了Xcode做到以下几点:

Step 1

第1步

step1

第1步

Step 2

第2步

step2

第2步

Step 3

第 3 步

step3

第三步

回答by Esqarrouth

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:

它包含在此 repo 中,请查看:

https://github.com/goktugyil/EZSwiftExtensions

https://github.com/goktugyil/EZSwiftExtensions

回答by tanaschita

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

回答by Sandip Patel - SM

Programmatically display version and build number - Swift 4.0

以编程方式显示版本和内部版本号 - 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) )"

回答by Sivabalaa Jothibose

You can also use like

你也可以使用像

var appVersion: String {
  //versionNumber
  let versionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "1.0"

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

  return "Version: #\(versionNumber) (#\(buildNumber))"
}

Then you can use anywhere when you declare Globally.

然后你可以在全局声明时在任何地方使用。

print(appVersion)

Output:

输出:

Version: #1.0 (#1.0.3)

回答by Amr Angry

I made an extension for Bundle so it will be easy to use

我为 Bundle 做了一个扩展,所以它很容易使用

extension Bundle {
    var releaseVersionNumber: String? {
        return infoDictionary?["CFBundleShortVersionString"] as? String
    }

    var buildVersionNumber: String? {
        return infoDictionary?["CFBundleVersion"] as? String
    }

    var applicationName: String {
        return infoDictionary?["CFBundleDisplayName"] as? String ?? "ADKATech"
    }

    var applicationReleaseDate: String {
        return infoDictionary?["ApplicationReleaseDate"] as? String ?? Date().description
    }

    var applicationReleaseNumber: Int {
        return infoDictionary?["ApplicationReleaseNumber"] as? Int ?? 0
    }

    var releaseVersionNumberPretty: String {
        return "\(releaseVersionNumber ?? "1.0.0")"
    }

    var buildVersionNumberPretty: String {
        return "\(buildVersionNumber ?? "1")"
    }
}