Skip to content
DeveloperMemos

Get version number programmatically with Swift

iOS, Swift1 min read

Following on from yesterday's post about TestFlight is another post today about getting an app's version number programmatically with Swift.

This one is pretty simple too, it's just a matter of reading Bundle's infoDictionary property:

1Bundle.main.infoDictionary?["CFBundleShortVersionString"]

You can create an extension on Bundle like this to make things a little cleaner:

1extension Bundle {
2 var versionNumber: String? {
3 infoDictionary?["CFBundleShortVersionString"] as? String
4 }
5}

I also added another extension to my project to make things even quicker(I usually show this information on the settings screen):

1extension Bundle {
2 var versionNumber: String? {
3 infoDictionary?["CFBundleShortVersionString"] as? String
4 }
5 var formattedVersion: String {
6 "v\(versionNumber ?? "1.0.0")"
7 }
8}

As a bonus you can also get the build number('Build' in Xcode) in a similar fashion:

1extension Bundle {
2 var buildVersionNumber: String? {
3 infoDictionary?["CFBundleVersion"] as? String
4 }
5}