Swift
iOS development
App version
Build number
Mobile app development

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In iOS app development, it's often necessary to retrieve versioning details of an app programmatically. This often includes the app's version number (e.g. 1.0.0) and build number (e.g. 100). These are typically set within the app's Info.plist file. Having access to these properties can be critical, especially for debugging, user feedback, and analytics.

In this article, you'll learn how to extract the app version and build number using Swift. You'll be introduced to technical concepts and code examples that demonstrate how to achieve this in a standard iOS app.

Understanding CFBundleShortVersionString and CFBundleVersion

Before diving into code, it's important to understand two key properties from the app's Info.plist file:

  • CFBundleShortVersionString: This is a user-facing version number (e.g., 1.0.0), representing the release version of the app.
  • CFBundleVersion: This is the build number (e.g., 100), typically used for internal reference to track builds.

Both properties are string types and are required to uniquely identify a specific version of the app.

Retrieving Version and Build Number

Accessing the Info.plist

The app's version and build number are defined in the Info.plist file. To access these values programmatically in Swift, you can use the Bundle class, specifically the main bundle associated with your application.

Here's how you can access these values:

swift
1import Foundation
2
3// Function to retrieve app's version and build number
4func getAppVersionAndBuild() -> (version: String, build: String) {
5    // Access the main bundle
6    if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String,
7       let build = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
8        return (version, build)
9    }
10    return ("Unknown", "Unknown")
11}
12
13// Example usage
14let (appVersion, appBuild) = getAppVersionAndBuild()
15print("App Version: \(appVersion), Build: \(appBuild)")

Explanation

  1. Bundle.main: Represents the top-level directory where the app resources are located.
  2. infoDictionary: A dictionary containing the key-value pairs listed in the Info.plist file.
  3. Subscripts ["CFBundleShortVersionString"] and ["CFBundleVersion"]: Used to retrieve specific values from the infoDictionary.

Using the Version and Build Number

Displaying in UI

You might want to display this information within the app interface, particularly in settings or help screens:

swift
1import UIKit
2
3class SettingsViewController: UIViewController {
4    
5    override func viewDidLoad() {
6        super.viewDidLoad()
7        
8        let (appVersion, appBuild) = getAppVersionAndBuild()
9        let versionLabel = UILabel()
10        versionLabel.text = "Version \(appVersion) (Build \(appBuild))"
11        versionLabel.frame = CGRect(x: 20, y: 100, width: 300, height: 50)
12        
13        self.view.addSubview(versionLabel)
14    }
15}

Sending to Server

For debugging and analytics, you might need to send this information to a server:

swift
1func sendVersionToServer() {
2    let (appVersion, appBuild) = getAppVersionAndBuild()
3    let url = URL(string: "https://api.example.com/version")!
4    var request = URLRequest(url: url)
5    request.httpMethod = "POST"
6    
7    let json: [String: String] = ["version": appVersion, "build": appBuild]
8    let jsonData = try! JSONSerialization.data(withJSONObject: json, options: [])
9    
10    let task = URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in
11        // Handle response
12    }
13    task.resume()
14}

Summary

Here's a concise table summarizing how to access the version and build number:

PropertyKeyDescriptionExample Value
Version NumberCFBundleShortVersionStringUser-facing version number1.0.0
Build NumberCFBundleVersionInternal build reference number100
Retrieval ClassBundle.mainInfo.plist access agent inside the app-
Retrieval MethodinfoDictionary?Method to access plist key-value pairs-

Conclusion

Accessing the app version and build number is a simple yet crucial task in iOS development. It allows developers to manage releases efficiently and improve user experience by providing important metadata for troubleshooting and feature updates. By implementing the strategies outlined in this guide, you can reliably retrieve this information and utilize it for various purposes within your app.


Course illustration
Course illustration

All Rights Reserved.