585

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.

How can I return the version and build number using Swift?

4
  • stackoverflow.com/questions/7608632/… Commented Sep 22, 2014 at 0:42
  • 8
    That's Objective-C, not Swift. Commented Sep 22, 2014 at 2:38
  • 12
    Be sure to not confuse CFBundleVersion & CFBundleShortVersionString`. The first is your build version. The other is version number. See here for more info Commented Nov 7, 2016 at 22:24
  • let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String. let build = Bundle.main.infoDictionary?["CFBundleVersion"] as? String Commented Feb 4, 2023 at 15:53

34 Answers 34

1
2
1

SWIFT 4

//First get the nsObject by defining as an optional AnyObject

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

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

let version = nsObject as! String 
Sign up to request clarification or add additional context in comments.

Comments

0

For Swift 2.0

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

Comments

0
if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String { lblVersion.text = "Version \(version)" } 

Comments

0

for anyone interested, there's a nice and neat library called SwifterSwift available at github and also fully documented for every version of swift (see swifterswift.com).

using this library, reading app version and build number would be as easy as this:

import SwifterSwift let buildNumber = UIApplication.shared.buildNumber let version = UIApplication.shared.version 

1 Comment

In 2020, it's let buildNumber = UIApplication.shared.buildNumber and let version = UIApplication.shared.version
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.