Loading...

How to display the app version and build number in a macOS/iOS SwiftUI app?

question swiftui macos
Ram Patra Published on October 15, 2023

To display both the app version and build number in a SwiftUI macOS/iOS app, you can use the Bundle class to access information from the app’s Info.plist file. The Info.plist file contains various details about your application, including its version and build number. Here’s how you can do it:

import SwiftUI

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    var body: some View {
        VStack {
            Text("App Version: \(getAppVersion())")
            Text("Build Number: \(getBuildNumber())")
        }
    }

    func getAppVersion() -> String {
        if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
            return appVersion
        }
        return "Unknown"
    }

    func getBuildNumber() -> String {
        if let buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
            return buildNumber
        }
        return "Unknown"
    }
}

In this code:

  1. We create a function getAppVersion() to retrieve the app version using the key "CFBundleShortVersionString" from the Info.plist file.
  2. We create a function getBuildNumber() to retrieve the build number using the key "CFBundleVersion" from the Info.plist file.
  3. Both values are displayed in separate Text views within the ContentView.

This will display both the app version and build number in your SwiftUI macOS or iOS app’s user interface. Make sure to replace the keys with the actual keys used in your Info.plist if they are different.

This is handy when displaying the app’s version in the About window. You do not need to update it manually every time you release a new version. Here are some screenshots from my own apps:

todobar about window

presentify about window

Ram Patra Published on October 15, 2023
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui macos September 3, 2024 How to open and close windows programmatically in SwiftUI?

To open or close a window programmatically from outside that window using environment variables, you need to leverage the new openWindow (macOS 13+) and dismissWindow (macOS 14+) environment variables. This environment variables allow you to programmatically open and close a window by its identifier.

question swiftui swift September 8, 2024 How to loop through an enum in SwiftUI?

In SwiftUI, looping through an enum is not directly possible without some extra work because enums in Swift don’t inherently support iteration. However, you can achieve this by making the enum CaseIterable, which automatically provides a collection of all cases in the enum.

question swiftui swift October 30, 2023 How to convert Color type to hex and vice-versa while retaining alpha information?

The below should work both on macOS and iOS with one minor change. That is, use UIColor instead of NSColor if you’re planning to use it for iOS.