To call openSettingsLegacy() from any subview, you must first attach the .openSettingsAccess() view modifier to a base view in your view hierarchy (typically the root view of your WindowGroup).
Step 1: Attach the modifier to your App's root view
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.openSettingsAccess()
}
Settings { SettingsView() }
}
}
Step 2: Use the environment method in a subview
struct ContentView: View {
@Environment(\.openSettingsLegacy) private var openSettingsLegacy
var body: some View {
Button("Open Settings") { try? openSettingsLegacy() }
}
}
Note on Xcode 16+:
Xcode 16 makes Apple's openSettings environment method public. For projects targeting macOS 14+, you may use the native openSettings. Use openSettingsLegacy() for compatibility with older macOS versions.
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.openSettingsAccess()
}
Settings { SettingsView() }
}
}
struct ContentView: View {
@Environment(\.openSettingsLegacy) private var openSettingsLegacy
var body: some View {
Button("Open Settings") { try? openSettingsLegacy() }
}
}