使用 Swift 获取版本和构建信息

我正试图获取主 NSBundle的访问权,以检索 版本建造信息。问题是,我想在 Swift 中试试,我知道如何在 Objective-C 中检索它:

text = [NSBundle.mainBundle.infoDictionary objectForKey:@"CFBundleVersion"];

但是我不知道从哪里开始使用 Swift,我尝试用新语法编写它,但是没有用。

85972 次浏览

What was wrong with the Swift syntax? This seems to work:

if let text = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
print(text)
}

For the final release of Xcode 6 use

NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as? String

The "?" character after infoDictionary is important here

Swift 3/4 Version

func version() -> String {
let dictionary = Bundle.main.infoDictionary!
let version = dictionary["CFBundleShortVersionString"] as! String
let build = dictionary["CFBundleVersion"] as! String
return "\(version) build \(build)"
}

Swift 2.x Version

func version() -> String {
let dictionary = NSBundle.mainBundle().infoDictionary!
let version = dictionary["CFBundleShortVersionString"] as String
let build = dictionary["CFBundleVersion"] as String
return "\(version) build \(build)"
}

as seen here.

[Update: Xcode 6.3.1] I tried all of the above and none of these work in Xcode 6.3.1 but I found that this does:

(NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as? String)!

In swift, I would make it as extension for UIApplication, like this:

extension UIApplication {


func applicationVersion() -> String {


return NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
}


func applicationBuild() -> String {


return NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleVersionKey as String) as! String
}


func versionBuild() -> String {


let version = self.applicationVersion()
let build = self.applicationBuild()


return "v\(version)(\(build))"
}
}

Then you can just use following to get everything you need:

let version = UIApplication.sharedApplication.applicationVersion() // 1
let build = UIApplication.sharedApplication.applicationBuild() // 80
let both = UIApplication.sharedApplication.versionBuild() // 1(80)

Another option is to define in the AppDelegate the variables:

var applicationVersion:String {
return NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
}
var applicationBuild:String  {
return NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleVersionKey as String) as! String
}
var versionBuild:String  {
let version = self.applicationVersion
let build = self.applicationBuild
return "version:\(version) build:(\(build))"
}

that can be referenced as variables in the AppDelegate

Swifty way for AppName, AppVersion and BuildNumber...

if let dict = NSBundle.mainBundle().infoDictionary {
if let version = dict["CFBundleShortVersionString"] as? String,
let bundleVersion = dict["CFBundleVersion"] as? String,
let appName = dict["CFBundleName"] as? String {
return "You're using \(appName) v\(version) (Build \(bundleVersion))."
}
}

This code works for Swift 3, Xcode 8:

let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "0"
let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "0"

Swift 3 :

let textVersion
= Bundle.main.infoDictionary?["CFBundleVersion"] as? String

Here is simple way to get Build and version.

For Swift 4.X

 if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
print(version)
}


if let build = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
print(build)
}

For Objective C

NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];


NSString * currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

Let me know if any issue. This is working for me.

SWIFT 3 Version

if let infoPath = Bundle.main.path(forResource: "Info.plist", ofType: nil),
let infoAttr = try? FileManager.default.attributesOfItem(atPath: infoPath),
let infoDate = infoAttr[.creationDate] as? Date
{
return infoDate
}
return Date()

For Swift 3,Replace NSBundle with Bundle and mainBundle is replaced simply by main.

let AppVersion = Bundle.main.infoDictionary!["CFBundleVersion"] as! String

//Returns app's version number

public static var appVersion: String? {
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}

//Return app's build number

public static var appBuild: String? {
return Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as? String
}

Get version from Framework's bundle

To have result for framework you can use

[Access to Framework bundle]

//inside framework
let version = bundle.infoDictionary?["CFBundleShortVersionString"] as? String

Swift 5.0

I created a wrapper for Swift 5 to get some app related strings at one place in all my apps, called AppInfo.

struct AppInfo {


var appName : String {
return readFromInfoPlist(withKey: "CFBundleName") ?? "(unknown app name)"
}


var version : String {
return readFromInfoPlist(withKey: "CFBundleShortVersionString") ?? "(unknown app version)"
}


var build : String {
return readFromInfoPlist(withKey: "CFBundleVersion") ?? "(unknown build number)"
}


var minimumOSVersion : String {
return readFromInfoPlist(withKey: "MinimumOSVersion") ?? "(unknown minimum OSVersion)"
}


var copyrightNotice : String {
return readFromInfoPlist(withKey: "NSHumanReadableCopyright") ?? "(unknown copyright notice)"
}


var bundleIdentifier : String {
return readFromInfoPlist(withKey: "CFBundleIdentifier") ?? "(unknown bundle identifier)"
}


var developer : String { return "my awesome name" }


// lets hold a reference to the Info.plist of the app as Dictionary
private let infoPlistDictionary = Bundle.main.infoDictionary


/// Retrieves and returns associated values (of Type String) from info.Plist of the app.
private func readFromInfoPlist(withKey key: String) -> String? {
return infoPlistDictionary?[key] as? String
}
}

You can use it like so:

print("The apps name = \(AppInfo.appname)")

Swift 100% working tested

You can get that easily by using single variable and make it public. You can use it everywhere you want.

(I am getting here User Agent for API header)

public let userAgent: String = {
if let info = Bundle.main.infoDictionary {
let executable = info[kCFBundleExecutableKey as String] as? String ?? "Unknown"
let bundle = info[kCFBundleIdentifierKey as String] as? String ?? "Unknown"
let appVersion = info["CFBundleShortVersionString"] as? String ?? "Unknown"
let appBuild = info[kCFBundleVersionKey as String] as? String ?? "Unknown"


let osNameVersion: String = {
let version = ProcessInfo.processInfo.operatingSystemVersion
let versionString = "\(version.majorVersion).\(version.minorVersion).\(version.patchVersion)"


let osName: String = {
#if os(iOS)
return "iOS"
#elseif os(watchOS)
return "watchOS"
#elseif os(tvOS)
return "tvOS"
#elseif os(macOS)
return "OS X"
#elseif os(Linux)
return "Linux"
#else
return "Unknown"
#endif
}()


return "\(osName) \(versionString)"
}()




return "\(executable)/\(appVersion) (\(bundle); build:\(appBuild); \(osNameVersion)) "
}


return "MyApp"
}()

Output:

"User-Agent": "MyApp/4.6.0 (com.app.myapp; build:4.6.0.0; iOS 15.2.0) "