When my Mac OS app quits I want it to ask the user “Are you sure you want to quit [Yes] [No]”.
I have tried this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
// Install a custom quit event handler
NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager];
[appleEventManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication];
}
// Handler for the quit apple event
- (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent {
// Insert YES/NO-dialog here
// if(blahahaha..
//[NSApp terminate:self];
}
But it only catch the quit that comes when right-clicking on my app on the dock and then choosing “Quit”. If I press Cmd-Q or choose Quit from the app menu my handler is not invoked…
Not all these cases involve Apple Events. Instead, handle the app delegate method
-applicationShouldTerminate:.