是否有办法在swift代码中获取产品版本号,例如
println("Running version \($PRODUCT_VERSION)")
我必须定义一些standartUserDefaults,它们绑定到Version和build number,我不喜欢在构建时将它们放入我的代码中。
NSProcessInfo没有内部版本号。 对我有什么建议吗?
答案 0 :(得分:5)
您可以通过此Info.plist
获取此类信息:
let version = NSBundle.mainBundle().infoDictionary?[kCFBundleVersionKey]
let build = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"]
println(version) // product version
println(build) //product build
答案 1 :(得分:2)
SWIFT 4.2
let appVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as? String
let build = Bundle.main.infoDictionary?["CFBundleVersion"] as? String
let buildString = "Version: \(appVersion ?? "").\(build ?? "")"
答案 2 :(得分:2)
在Swift 5.3中:
extension UIApplication {
static var appVersion: String? {
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}
}
用法:
UIApplication.appVersion!