Get App version in Swift

hejliang

Liang Wang

Posted on February 5, 2024

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.

πŸ’– πŸ’ͺ πŸ™… 🚩
hejliang
Liang Wang

Posted on February 5, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related