Getting version and build information with Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
App version details are often shown on a settings screen, attached to crash reports, or included in support logs. In Swift, that information usually comes from the app bundle, specifically the values stored in Info.plist.
Where Version And Build Values Come From
Two keys matter most:
- '
CFBundleShortVersionStringis the marketing version seen by users, such as2.4.1.' - '
CFBundleVersionis the internal build number, often incremented every release or CI run.'
Both values live in the main application bundle, so you can read them through Bundle.main. That keeps the information in one place instead of hard-coding version strings throughout the app.
Reading The Values In Swift
The simplest solution is a small helper that reads both keys and returns safe fallback values if the keys are absent.
This approach is better than force-casting because missing or malformed bundle values will not crash the app. A support screen can show AppInfo.fullVersion, while logging code can record version and build separately.
Showing Version Information In The UI
Once the helper exists, displaying the value is straightforward. Here is a small SwiftUI example:
The same pattern works in UIKit:
Keeping the formatting in one helper prevents different screens from showing slightly different version strings.
Using Version Data In Logging And Diagnostics
Version information is not just for display. It is useful whenever you need to correlate runtime behavior with a specific release. A common pattern is to log it once during app startup:
That makes support investigations easier because bug reports can be tied to an exact app build. If you use analytics or crash reporting SDKs, pass the same values into their metadata APIs so every event is tagged consistently.
Main Bundle Versus Other Bundles
One subtle detail is bundle selection. Bundle.main refers to the app bundle, which is correct for most iOS and macOS apps. If the code lives in a framework or test target, the relevant version information may be stored in a different bundle.
For example, a reusable framework might need:
That distinction matters in modular projects. Otherwise you may accidentally display the host app version when you intended to read a framework version.
Common Pitfalls
- Confusing
CFBundleShortVersionStringwithCFBundleVersion. One is the user-facing release number, the other is the internal build number. - Force-casting bundle values. Missing keys should not crash the app.
- Reading from
Bundle.maininside a framework when the data actually lives in the framework bundle. - Hard-coding version strings in code or UI labels. The bundle should stay the single source of truth.
- Forgetting to update the build number in release automation, which makes support and crash triage harder.
Summary
- In Swift, version and build metadata usually comes from
Info.plist. - Use
Bundle.main.object(forInfoDictionaryKey:)to readCFBundleShortVersionStringandCFBundleVersion. - Wrap the lookup in a small helper so UI and logging code stay consistent.
- Be careful about which bundle you read from in frameworks, extensions, and tests.
Related reading
- Getting version and build information with Swift
- Git ignore file for Xcode projects
- Git ignore file for Xcode projects
- Given a view, how do I get its viewController?
- Giving UIView rounded corners
- Global constants file in Swift
- Global constants file in Swift
- GMSGroundOverlay animating - should I be using a CATiledLayer?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.