I have a Mac app whose applicationWillTerminate:(NSNotification *) handler is not called when the app closes down. There is some c++ memory cleanup code that must be called if the program is shut down, so I need some way to do this.
The reason it doesn't get called seems to be related to my adding the following key to Info.plist:
<key>LSUIElement</key>
<true/>
I've read the other questions and answers on this topic, for example applicationWillTerminate: not being called and applicationWillTerminate not called (the accepted correct answer did not work). applicationDidEnterBackground() is never fired either. applicationDidFinishLaunching() always fires ok. I return NSTerminateNow if applicationShouldTerminate() is ever called, but it isn't called either.
For the curious, the app is a Chrome native messaging host and is launched programmatically. And it can be shut down on me if the pipe closes, so I really need some kind of event or notification or something that will reliably allow me to execute some code on shutdown. My app needs to remain invisible and it needs not to appear in application switcher if the user uses Command-Tab.
UPDATE:
I've noticed from the beginning that the following is logged to the Console app when I terminate my app from the Activity Monitor:
com.apple.xpc.launchd[1]: (com.allinlearning.ailhost.67908[13017]) Service exited due to signal: Terminated: 15
Which relates directly to Peter Hosey's answer for this SO question: applicationWillTerminate does not get invoked. So I immediately added the NSSupportsSuddenTermination key to my Info.plist to disable sudden termination, hoping (as Peter's answer implies) that applicationWillTerminate will be given time to fire. Alas, it did not. But it does perhaps clarify things. If indeed that console statement means my app is being terminated, then how can I get a chance to execute some code before The End?