Loading...

How to open the Settings view in a SwiftUI app on macOS 14.0 (Sonoma)?

question swiftui swift
Ram Patra Published on September 30, 2023

In macOS 14.0 (Sonoma), Apple removed support for NSApp.sendAction to open the Settings view in your SwiftUI app. You now have to use SettingsLink like below:

if #available(macOS 14.0, *) { // open Settings view on macOS 14.0+
    SettingsLink {
        HStack {
            Image(systemName: "gearshape")
            Text("Preferences")
        }
    }
} else { // open Settings view on macOS 13.0
    Button {
        NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil)
    } label: {
        HStack {
            Image(systemName: "gearshape")
            Text("Preferences")
        }
    }
}

And, if you want to support even older versions of macOS, you have to add one more if condition and open the Settings view like below:

NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil)
Ram Patra Published on September 30, 2023
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui April 2, 2024 How to declare an array of Views in SwiftUI?

In SwiftUI, you can declare an array of View using the standard Swift array syntax. Here’s how you can do it:

question swiftui iOS April 2, 2024 How to open a second view from first view in iOS using SwiftUI?

In SwiftUI, you can open another view (or navigate to another view) on the click of a button by utilizing navigation views and navigation links. Here’s a basic example of how to achieve this:

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.