DEV Community

Liang Wang
Liang Wang

Posted on

Get App version in Swift

Previously I hardcoded the appVersion and put it into the constant file, but I keep forgetting to update it when I make a new build or update the version in the Settings.

Today I found out that there is a good question and answer from StackOverflow that fixes my problem programmatically, which is nice to have, as I am lazy to remember too much tiny stuff here and there.

link

Solution:
let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String

Example:
Text(appVersion ?? "Unknown version")

Tip: (also from the answers)
Be sure to not confuse CFBundleVersion & CFBundleShortVersionString.
The first is your build version.
The other is version number.

Top comments (0)