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

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on October 15, 2023
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui swift August 31, 2024 @StateObject vs @ObservedObject in SwiftUI

In SwiftUI, both @StateObject and @ObservedObject are property wrappers used to manage state in your views, specifically when working with objects that conform to the ObservableObject protocol. However, they serve slightly different purposes and have different use cases. Here’s a breakdown:

question swiftui swift September 2, 2024 Combine in SwiftUI and how you can rewrite the same code using async await

Combine is Apple’s declarative framework for handling asynchronous events and data streams in Swift. Introduced in SwiftUI and iOS 13, Combine leverages reactive programming principles, allowing developers to process values over time and manage complex asynchronous workflows with clarity and efficiency.