Reputation: 3980
I need to add a quit-button to my application that runs from the menubar in mac. How do I programmatically quit an application in mac?
Upvotes: 51
Views: 21985
Reputation: 926
Written in Swift:
NSApp.terminate(self)
or
NSApplication.shared.terminate(self)
Upvotes: 3
Reputation: 5148
In some case you cannot close app when call [NSApp terminate:self];
. Like when showing NSAlert as sheet on the doc window (NSAlert -beginSheetModalForWindow:completionHandler:
) ...
You can close all window and alert before call terminate, like following code:
for (NSWindow *window in [NSApplication sharedApplication].windows) {
[window close];
}
[NSApp terminate:self];
Upvotes: 4
Reputation: 59277
There is a simpler form to quit from code:
[NSApp terminate:self];
But as you're adding a button, all you have to do is to control drag from your button to the Application icon and connect the method terminate:
.
Upvotes: 85