如何使用Swift获取App版本和内部版本号?

卢毅
2023-12-01

本文翻译自:How do I get the App version and build number using Swift?

I have an IOS app with an Azure back-end, and would like to log certain events, like logins and which versions of the app users are running. 我有一个带有Azure后端的IOS应用程序,并且想记录某些事件,例如登录名和正在运行的应用程序版本。

How can I return the version and build number using Swift? 如何使用Swift返回版本和内部版本号?


#1楼

参考:https://stackoom.com/question/1kwkB/如何使用Swift获取App版本和内部版本号


#2楼

EDIT 编辑

Updated for Swift 4.2 为Swift 4.2更新

let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String

EDIT 编辑

As pointed out by @azdev on the new version of Xcode you will get a compile error for trying my previous solution, to solve this just edit it as suggested to unwrap the bundle dictionary using a ! 正如@azdev在新版本的Xcode上指出的那样,您尝试我以前的解决方案时会遇到编译错误,要解决该问题,只需按照建议的方法对其进行编辑即可使用!来解开bundle字典。

let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]

End Edit 结束编辑

Just use the same logic than in Objective-C but with some small changes 只需使用与Objective-C相同的逻辑,但有一些小的更改

//First get the nsObject by defining as an optional anyObject
let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary["CFBundleShortVersionString"]

//Then just cast the object as a String, but be careful, you may want to double check for nil
let version = nsObject as String

I hope this helps you out. 我希望这能够帮到你。

David 大卫


#3楼

I know this has already been answered but personally I think this is a little cleaner: 我知道已经回答了这个问题,但就我个人而言,我认为这更干净一些:

Swift 3.0: Swift 3.0:

 if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
    self.labelVersion.text = version
}

Swift <2.3 迅捷<2.3

if let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String {
    self.labelVersion.text = version
}

This way, the if let version takes care of the conditional processing (setting the label text in my case) and if infoDictionary or CFBundleShortVersionString are nil the optional unwrapping will cause the code to be skipped. 这样,if let版本负责条件处理(在我的情况下设置标签文本),并且如果infoDictionary或CFBundleShortVersionString为nil,则可选的展开将导致代码被跳过。


#4楼

I also know this has already been answered but I wrapped up the previous answers: 我也知道这已经得到回答,但是我总结了以前的答案:

(*)Updated for extensions (*)已更新扩展名

extension Bundle {
    var releaseVersionNumber: String? {
        return infoDictionary?["CFBundleShortVersionString"] as? String
    }
    var buildVersionNumber: String? {
        return infoDictionary?["CFBundleVersion"] as? String
    }
    var releaseVersionNumberPretty: String {
        return "v\(releaseVersionNumber ?? "1.0.0")"
    }
}

Usage: 用法:

someLabel.text = Bundle.main.releaseVersionNumberPretty

@Deprecated: Old answers @不推荐使用:旧答案

Swift 3.1 : Swift 3.1

class func getVersion() -> String {
    guard let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String else {
        return "no version info"
    }
    return version
}

For older versions : 对于旧版本

class func getVersion() -> String {
    if let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String {
        return version
    }
    return "no version info"
}

So if you want to set label text or want to use somewhere else; 因此,如果您要设置标签文本或要在其他地方使用,请执行以下操作:

self.labelVersion.text = getVersion()

#5楼

Updated for Swift 3.0 为Swift 3.0更新

The NS -prefixes are now gone in Swift 3.0 and several properties/methods have changed names to be more Swifty. NS前缀现在在Swift 3.0中消失了,并且一些属性/方法已将名称更改为更Swifty。 Here's what this looks like now: 这是现在的样子:

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

Bundle.main.releaseVersionNumber
Bundle.main.buildVersionNumber

Old Updated Answer 旧的更新答案

I've been working with Frameworks a lot since my original answer, so I wanted to update my solution to something that is both simpler and much more useful in a multi-bundle environment: 自从我最初的回答以来,我就一直在使用Frameworks,所以我想将我的解决方案更新为在多捆绑环境中既简单又有用的解决方案:

extension NSBundle { var releaseVersionNumber: String? { return self.infoDictionary?["CFBundleShortVersionString"] as? String } var buildVersionNumber: String? { return self.infoDictionary?["CFBundleVersion"] as? String } }

Now this extension will be useful in apps to identify both the main bundle and any other included bundles (such as a shared framework for extension programming or third frameworks like AFNetworking), like so: 现在,此扩展将在应用程序中有用,以识别主捆绑包和任何其他包含的捆绑包(例如,用于扩展编程的共享框架或诸如AFNetworking的第三种框架),如下所示:

NSBundle.mainBundle().releaseVersionNumber NSBundle.mainBundle().buildVersionNumber // or... NSBundle(URL: someURL)?.releaseVersionNumber NSBundle(URL: someURL)?.buildVersionNumber

Original Answer 原始答案

I wanted to improve on some of the answers already posted. 我想改善已经发布的一些答案。 I wrote a class extension that can be added to your tool chain to handle this in a more logical fashion. 我写了一个类扩展,可以将其添加到您的工具链中,以更合乎逻辑的方式进行处理。

extension NSBundle { class var applicationVersionNumber: String { if let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"]

as? 如? String { return version } return "Version Number Not Available" } 字符串{返回版本}返回“版本号不可用”}

class var applicationBuildNumber: String { if let build = NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as? String { return build } return "Build Number Not Available" } }

So now you can access this easily by: 因此,现在您可以通过以下方式轻松访问此地址:

let versionNumber = NSBundle.applicationVersionNumber

#6楼

For Swift 1.2 it's: 对于Swift 1.2,它是:

let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
let build = NSBundle.mainBundle().infoDictionary!["CFBundleVersion"] as! String
 类似资料: