Search code examples
swiftcocoansmenunsmenuitemresponder-chain

macOS - Menu actions do not trigger any action if the app is not active


I am building this app that will sit on the menu bar.

So, as normally is done with this kind of app, you adjust the statusItem menu to be the menu of your app.

The items on that menu trigger actions on the first responder, that in my case is the viewController of that app.

This is the problem: this works if the app is active but if the app is not active or hidden, this fails. I guess the actions triggered by the menu items will fire to the responder chain but there is no one listening, because the app is hidden or not active.

How do I do in that case? Should I create a singleton and add that singleton to the responder chain? I say singleton because that would be always loaded in memory. Does this singleton have to be a subclass of some special class?

How to do if actions from this menu should trigger stuff in the viewController?


Solution

  • You should be able to handle first responder in the AppDelegate, which should work if the app is not active.