How do I get the App version and build number using Swift? How do I get the App version and build number using Swift? ios ios

How do I get the App version and build number using Swift?


EDIT

Updated for 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 !

let nsObject: AnyObject? = Bundle.main.infoDictionary!["CFBundleShortVersionString"]

End Edit

Just use the same logic than in Objective-C but with some small changes

//First get the nsObject by defining as an optional anyObjectlet nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary["CFBundleShortVersionString"]//Then just cast the object as a String, but be careful, you may want to double check for nillet version = nsObject as! String

I hope this helps you out.

David


I know this has already been answered but personally I think this is a little cleaner:

Swift 3.0:

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

Swift <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.


Updated for Swift 3.0

The NS-prefixes are now gone in Swift 3.0 and several properties/methods have changed names to be more 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.releaseVersionNumberBundle.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:

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:

NSBundle.mainBundle().releaseVersionNumberNSBundle.mainBundle().buildVersionNumber// or...NSBundle(URL: someURL)?.releaseVersionNumberNSBundle(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