How to Get App Version and Build Number in Swift

Sometimes you may want to display the app version and build number within your iOS app.

In this article, we'll explain how to retrieve the version and build number of an iOS app in Swift.

Check the App Version and Build Number in Xcode

First, let's check the version and build number of your iOS app in Xcode.

Open your iOS project in Xcode.

Click on the project name at the top of the navigator area on the left, then click the project name under TARGETS, and select the General tab.

In the Identity section, below the Bundle Identifier, you'll see the app's Version and Build. These can also be edited here.

Check app version and build number in Xcode 1


For this example, we created a new iOS app and set the Version to 1.2 and the Build to 3 for testing.


Get the App Version and Build Number in Swift

There are two ways to retrieve the app version and build number in Swift.


The first method uses the object(forInfoDictionaryKey:) method of the main bundle class.

This method retrieves a value from the app's Information Property List by specifying its key.

The key for the version is CFBundleShortVersionString, and the key for the build is CFBundleVersion.


Let's use the object(forInfoDictionaryKey:) method to get the app's version and build number, and print them.

Add the following code to ViewController.swift:

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let version = getInfoDictionary("CFBundleShortVersionString")
        let build = getInfoDictionary("CFBundleVersion")   
        print("Version: \(version), Build: \(build)")
    }
    
    func getInfoDictionary (_ key: String) -> String {
        if let version = Bundle.main.object(forInfoDictionaryKey: key) as? String {
            return version
        } else {
            return ""
        }
    }
}

The getInfoDictionary() function takes a key and returns the corresponding value from the Information Property List.

On line 14, it uses Bundle.main.object(forInfoDictionaryKey:) to retrieve the value.

The return type of object(forInfoDictionaryKey:) is Any?, so it's cast to a String.

If the result is not nil, it returns the value; otherwise, it returns an empty string.


On line 8, we pass CFBundleShortVersionString to getInfoDictionary() to get the version value.

On line 9, we pass CFBundleVersion to getInfoDictionary() to get the build value.

On line 10, both values are printed.


If you run this on the simulator, the output window will show:

Version: 1.2, Build: 3

Check app version and build number in Xcode 2


The second method uses the infoDictionary property of the main bundle class.

You can retrieve values by specifying keys, for example: Bundle.main.infoDictionary?[key] or Bundle.main.infoDictionary![key].

The keys for version and build are the same: CFBundleShortVersionString and CFBundleVersion.


Update line 14 in ViewController.swift as follows:

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let version = getInfoDictionary("CFBundleShortVersionString")
        let build = getInfoDictionary("CFBundleVersion")   
        print("Version: \(version), Build: \(build)")
    }
    
    func getInfoDictionary (_ key: String) -> String {
        if let version = Bundle.main.infoDictionary?[key] as? String {
            return version
        } else {
            return ""
        }
    }
}

On line 14, we now use Bundle.main.infoDictionary to get the value from the Information Property List.


If you run this on the simulator, you'll again see the output:

Version: 1.2, Build: 3

Check app version and build number in Xcode 3


In this example, we retrieved the app's Version and Build number from the bundle. Similarly, you can also fetch other values defined in Info.plist by specifying their keys.

That's how to get the app version and build number in Swift.