How to get the build/version number of an iOS App?


In this post we will learn how to fetch and show the iOS build and version number

Step 1 − Open Xcode → New Project → Single View Application → Let’s name it “ShowBuildAndVersion”

Step 2 − Open Main.storyboard and add two labels as shown below.

Step 3 − Attach @IBOutLets for the two labels

@IBOutlet weak var buildLabel: UILabel!
@IBOutlet weak var versionLabel: UILabel!

Step 4 − Change the build and version from project settings.

Step 5 − In viewDidLoad of ViewController get the build and version number for infoDictionary of main bundle. Show it on the corresponding labels.

override func viewDidLoad() {
   super.viewDidLoad()
   if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
      versionLabel.text = "Version: \(version)"
   }
   if let build = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
      buildLabel.text = "Build: \(build)"
   }
}

The infoDictionary in main bundle contains these values, with ‘CFBundleShortVersionString’ and ‘CFBundleVersion’ keys.

We can refer these keys to get the version and build number respectively

Step 6 − Run the project, you should see the build and version number.


raja
Published on 11-Sep-2019 12:05:25
Advertisements