swift how to get app version

Swift: Get App Version - Easy Steps

By Mark Garcia
AD

When developing an app using Swift, it's important to manage and display the version number correctly. Users often look for this information to check for updates or identify the version they're using. Here's a comprehensive guide on how to get your app's version number in Swift.

Before we dive into the code, let's understand a bit about app versioning. In iOS, the app version is represented by a tuple of four integers, commonly known as the CFBundleVersion and CFBundleShortVersionString keys in the app's Info.plist file.

Accessing App Version Number

The first step in displaying the app's version number is to access it. Swift allows us to retrieval these values using the `Bundle.main` property and the `object(forInfoDictionaryKey:)` method.

AD
Learn Swift
Learn Swift
a man holding up a blue box with an image of a bird on it and the words build io's apps above him
a man holding up a blue box with an image of a bird on it and the words build io's apps above him
SwiftUI Basics For Beginners
SwiftUI Basics For Beginners

Remember to define your app's version number in your 'Info.plist' file. This ensures that your code can access the information.

Getting the Short Version String

To get the app's short version string (the visible version number users see in the App Store), use the following code:

Swift Tutorial 2026: Learn Swift From Absolute Zero : CodeWithChris
Swift Tutorial 2026: Learn Swift From Absolute Zero : CodeWithChris
SwiftUI Basics: Build Your First iOS App in Xcode Easily 📱
SwiftUI Basics: Build Your First iOS App in Xcode Easily 📱
How to Make an App: A Complete Beginner's Guide (2026) : CodeWithChris
How to Make an App: A Complete Beginner's Guide (2026) : CodeWithChris
let shortVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "Unknown"
print("Short Version: \(shortVersion)")

Getting the Full Version Number

To retrieve the full version number (used under the hood for updating and testing), use the following code:

let fullVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "Unknown"
print("Full Version: \(fullVersion)")

Displaying the App Version Number

Once you have accessed the app's version number, you can display it anywhere in your app. Here's how you can display it in your app's Settings view:

AD

Displaying in Settings

First, add a new Settings item in your 'Info.plist' file:

What Jobs can you get with Swift?
What Jobs can you get with Swift?
Swift vs Objective-C: Which One to Consider For Your Next iOS App? [2023 update]
Swift vs Objective-C: Which One to Consider For Your Next iOS App? [2023 update]
a screenshot of the swift chat and quick reference page with instructions on how to use it
a screenshot of the swift chat and quick reference page with instructions on how to use it
iPhone App Development: From Idea to App Store
iPhone App Development: From Idea to App Store
Learn SwiftUI and Create an iOS App From Scratch
Learn SwiftUI and Create an iOS App From Scratch
15 Places to Learn Swift Programming Language For Free
15 Places to Learn Swift Programming Language For Free
Key: ITSAppUsesNonExemptEncryption, Type: Boolean, Value: 0

Then, add a new extension to your 'UIApplication':

extension UIApplication {
    var appVersionNumber: String? {
        return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
    }
}

And access it like this:

let appVersion = UIApplication.shared.appVersionNumber
if let version = appVersion {
    print("App Version: \(version)")
}

Displaying in a Label

You can also display the app's version number in a UILabel as follows:

let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "Unknown"
appVersionLabel.text = "App Version: \(appVersion)"

By following these steps, you can easily manage and display your app's version number in Swift. Staying on top of versioning can help you provide a better user experience and make app updates easier.

Your First SwiftUI App: The War Card Game
Your First SwiftUI App: The War Card Game
an image of a program with the words object - oriented programming and other things in it
an image of a program with the words object - oriented programming and other things in it
Learn SwiftUI Basics - Tutorial Lessons
Learn SwiftUI Basics - Tutorial Lessons
AD