How do you make the Application window open when the dock icon is clicked?

Viewed 15473

I'm surprised this doesn't happen automatically, but I would like my applications window to open automatically when the Dock icon is clicked.

Just to clarify, when i open the app, the window automatically opens, but when I click the cross for the window but leave the App running, the window won't open when i click the dock icon.

6 Answers

Implement - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag in your app delegate. Check the documentation for the details of the return value.

Document based apps and non-document based apps behave slightly differently. If there are no open windows when the dock icon of a document based app is clicked then it will create a new document. If there are no open windows when the dock icon of a non-document based app is clicked then it will do nothing.

As others pointed, using applicationShouldHandleReopen method for reopening closed windows in non-document apps is the right way. The only change I want to add is a more flexible way to check what window must be re-displayed, by iterating through the NSApplication's list of visible and invisible .windows and checking for the required window.

func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {

    if flag == false {

        for window in sender.windows {

            if (window.delegate?.isKind(of: WelcomeWindowController.self)) == true {
                window.makeKeyAndOrderFront(self)
            }
        }
    }

    return true
}

Appendix

a) If window was hidden then it will be showed automatically when user will click on app's Dock icon, so no need to implement applicationShouldHandleReopen method.

b) Checked "Release when closed" option doesn't affect in any way the above behaviour.

A document based application will automatically open a new untitled document when the app becomes active, so I am assuming you are referring to a non-document based app.

Implement the applicationDidBecomeActive: method in your application delegate and open/show the window.

Edit:

Some information on Delegates.

Some information on Opening and Closing Windows and the NSWindow API

Related