onDisappear (or similar) for macOS Settings/Preferences screen

Hi All, this question has been asked before by others, without any success so far.

I'm using the following (standard) method to open the system provided settings screen for my macOS (only) app in SwiftUI.

var body: some Scene {
        Settings {
            PreferencesView()
        }
        if #available(macOS 13, *) {
            // MenuBarExtra requires macOS 13, but SettingsLink used in here requires macOS 14
            MenuBarExtra("...", image: "...") {
                ...
                // this one requires macOS 14
                if #available(macOS 14, *) {
                    SettingsLink() {
                        Text("Settings...")
                    }
                } else {
                    Button {
                        NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil)
                    } label: {
                        Image(systemName: "gear")
                    }
                }
            }
        }
    }

I want to know when the settings window closes. onDisappear does not work as the settings window is not really closed, but hidden. So the view stays active. All other hacks I have found neither work.

Does anyone have any idea?

Hi @brainchest .

I tested the behavior as well. I used ScenePhase to check the phase and noticed that I can see when it first becomes active, but nothing after that. Please file a bug report at https://feedbackassistant.apple.com and paste the FB number here. That's a great catch.

facing this exact problem on macOS 14. it seems to be fixed on macOS 15 but i would like this to work on 14 as well. is there any way to still know that a Settings SwiftUI view is being closed on macOS 14?

onDisappear (or similar) for macOS Settings/Preferences screen
 
 
Q