I'm building a music app for Mac using AppKit and Swift. I'm targeting macOS 11.0+.
When the app's last window is closed and then resigns active, my app is removed from the Dock, and terminated (when not attached to the debugger).
I'd like to stop this behaviour. When my app resigns active with no windows, it should remain in the Dock and continue running.
The current behaviour means that when music is playing, if the last window is closed (but not the app itself) and a user starts using a different app (e.g. Safari) the app is terminated. The expected behaviour would be to continue running and playing audio until the user explicitly quits the app.
I can't find this behaviour documented and can't find a way to fix this.
Thanks in advance for any help/pointers!
Notes:
- My
NSApplicationDelegate
returnsfalse
forapplicationShouldTerminateAfterLastWindowClosed
So! Asked around and was pointed to the NSSupportsAutomaticTermination
key in Info.plist
.
From the docs:
Automatic termination allows an app that is running to be terminated automatically by the system when certain conditions apply. Primarily, the app can be terminated when it is hidden or does not have any visible windows and is not currently being used.
Exactly what I was seeing!
Setting this to false
fixed the issue.
(Thanks to J**** and J**** for the help!)