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.
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:
Explanation
Bundle.mainaccesses the app's main bundle.object(forInfoDictionaryKey:)retrieves the value for the specified key.
Key Points Summary
| Key Points | Explanation |
CFBundleShortVersionString | Represents the version of the app. Displayed to users. |
CFBundleVersion | Represents the build number or internal release version. |
| Usage | Useful for debugging & user support. Version display. |
| Swift Access | Achieved 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 like1.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.plistkeys 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.

