Swift
iOS Development
App Version
Build Number
Software Development

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

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

To retrieve the app version and build number in a Swift application, we need to use the information stored in the app's Info.plist file, where various metadata about the app is stored. This metadata includes the version number (CFBundleShortVersionString) and the build number (CFBundleVersion). In this article, we'll explore how to programmatically access this information using Swift, and further discuss some related topics.

Accessing App Version and Build Number

In a Swift application, you can access your app's version and build number using the Bundle class. The Bundle class provides a programmatic interface for interacting with the app's bundle directory, which includes the Info.plist file.

Here's a step-by-step breakdown of how to retrieve this information:

1. Access the Main Bundle

The Bundle.main reference gives you access to the main bundle of your app, where the Info.plist resides.

2. Extract the Version and Build Number

You can use object(forInfoDictionaryKey:) method of the Bundle class to retrieve the value for keys CFBundleShortVersionString and CFBundleVersion.

3. Display the Information

This information can be used for display purposes, debugging, or analytics.

Example Code

Here's a sample code snippet that demonstrates how to fetch the app version and build number:

swift
1import Foundation
2
3// Fetch the App Version and Build Number
4if let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String,
5   let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String {
6    
7    print("App Version: \(appVersion)")
8    print("Build Number: \(buildNumber)")
9} else {
10    print("Could not retrieve version/build number.")
11}

Explanation

  • Bundle.main accesses the app's main bundle.
  • object(forInfoDictionaryKey:) retrieves the value for the specified key.

Key Points Summary

Key PointsExplanation
CFBundleShortVersionStringRepresents the version of the app. Displayed to users.
CFBundleVersionRepresents the build number or internal release version.
UsageUseful for debugging & user support. Version display.
Swift AccessAchieved via Bundle.main & object(forInfoDictionaryKey:).

Additional Details

Version and Build Number in Info.plist

The Info.plist file is a central repository for app configuration details. It's crucial to set CFBundleShortVersionString and CFBundleVersion correctly:

  • CFBundleShortVersionString: The version of your app as it will be marketed. Typically, this follows a semantic versioning pattern like 1.0.0.
  • CFBundleVersion: It denotes the internal version of the app. Each build should increment this number, even if the changes are minor.

Use Cases

  • User Support and Debugging: By knowing the app version and build number, developers can better diagnose and address issues raised by users.
  • Analytics and Metrics: Tracking app versions helps in understanding app usage and user retention among different app versions.

Access Beyond Main Bundle

If you work with multiple bundles, such as in a framework or separate module, you may need to specify the required bundle explicitly instead of using Bundle.main.

Common Pitfalls

  • Always ensure that the Info.plist keys are set correctly in your Xcode project.
  • Casting issues can occur if the value is not the expected type (e.g., String). It's good practice to use optional binding (as?) to safely downcast values.

Modify and Increment Build Numbers

For automated build systems or continuative integration, you can set up scripts within your Xcode project to auto-increment the build number during each build. This is commonly done using build phase scripts or external tools like Fastlane.

By understanding and programmatically accessing the app version and build number, developers not only streamline their build and deployment processes but also enhance the support ecosystem and analytics of the application.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.