Reopening an application's main window by clicking on the Dock Icon
When building Mac applications, Apple usually takes care of most of the default behaviours for you. One thing that Mac applications don’t do by default is reopening the application’s main window (if it has been closed), when the dock icon is pressed.
Although this information is very hard to find in the documentation, it is actually very easy to do. The method you need to find is:
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag;
This is an optional delegate method that your AppDelegate can choose to implement, and is called when the user presses your application’s dock icon. The BOOL flags indicates whether the application has any visible windows. To reopen your application’s main window, you need to have a pointer to it (In the example below assume that it is defined as NSWindow *window; in the header file). If you do have a pointer to it, then you simply need to implement the code below.
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag {
if (flag == NO) {
[window makeKeyAndOrderFront:self];
}
return YES;
}
It is as simple as that.