Joshua
Joshua

Reputation: 15510

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

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.

Upvotes: 47

Views: 16755

Answers (7)

Akmal
Akmal

Reputation: 7

https://developer.apple.com/forums/thread/706772?answerId=715063022#715063022

func applicationDidFinishLaunching(_ aNotification: Notification) 
{
    NSApplication.shared.delegate = self
}

func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
     //Now is working
    return true
}

Upvotes: 0

nsinvocation
nsinvocation

Reputation: 7637

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.

Upvotes: 11

Maximilian Melzer
Maximilian Melzer

Reputation: 31

A solutions to add to the accepted answer:

With the accepted answer the reopened window did not react to mouse events anymore.

When using the accepted answer you also have to make sure to uncheck "Release when closed" in the Attributes Inspector of the window in IB. This fixes the unresponsive window problem.

Upvotes: 3

Dipak Narigara
Dipak Narigara

Reputation: 1806

- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
{
    if (flag) {
        return NO;
    }
    else
    {
       [YourWindow makeKeyAndOrderFront:self];// Window that you want open while click on dock app icon
        return YES;
    }
}

Upvotes: 15

Mariano Cavallo
Mariano Cavallo

Reputation: 959

This is what I'm doing to get the main window of a non-document based app back to screen once it has been closed. I know this might not be the right way to do it but It's working for me so far.

Implemented this on the AppDelegate, window is defined as instance variable of the same object.

- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
    [window makeKeyAndOrderFront:self];
    return NO;
}

If anyone has a better solution please reply. Thanks!

Upvotes: 13

Benedict Cohen
Benedict Cohen

Reputation: 11920

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.

Upvotes: 68

Nathan Kinsinger
Nathan Kinsinger

Reputation: 25021

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

Upvotes: 4

Related Questions