0

I have created an App for Mac Desktop, it is working, but Apple rejected it because when we run the App and close it by using "X", the we can not re-open it from the dock though the App icon is still there but it doesn't open the App again and the main issue for that I am struggling is that "If we close the App then in the menu bar there is no option to open it" other App which I have seen does that.

What should I do?

halfer
  • 19,824
  • 17
  • 99
  • 186
Eager Beaver
  • 949
  • 3
  • 11
  • 20
  • 1
    What do you mean by "close it using X" ? Also, what is stopping you from debugging and fixing your app so that it works correctly ? After all, you wrote it, so you know how it works, right ? – Paul R Aug 11 '12 at 07:35
  • Close by using X means the X(close) button at left most corner of the App. – Eager Beaver Aug 11 '12 at 08:41
  • I am now able to re-launch the App from the Dock using this link:http://stackoverflow.com/questions/5498376/closing-mac-application-clicking-red-cross-on-top-and-reopening-by-clicking-do but how can I show option of Open in Menu bar? – Eager Beaver Aug 11 '12 at 08:46
  • 2
    So when you say 'X' you're getting confused with Windows and you mean the **red button** ? Unlike Windows that button should only close the *window* - it should **not** quit the app. Read the Apple UI guidelines - you will seriously annoy your users if you try to make a Mac OS X app behave like a Windows program. – Paul R Aug 11 '12 at 09:58
  • I have a related information to share. If you are using xcode 11.4 on MacOS 10.15 with Swift 5.2, this same problem exists in MacOS SwiftUI app. Adding following code inside AppDelegates.swift solves the issue. func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { if !flag{ window.makeKeyAndOrderFront(nil) } return true } – NikzJon Apr 17 '20 at 16:09

4 Answers4

4

Here's an answer for Swift 3:

Conform to NSWindowDelegate in your View Controller class. Then hide the window instead of closing it by overriding the following method.

self.view.window?.delegate = self

func windowShouldClose(_ sender: Any) -> Bool {
    NSApplication.shared().hide(self)
    return false
}

Then unhide the application when the app icon is clicked in the dock.

func applicationDidBecomeActive(_ notification: Notification) {
    NSApplication.shared().unhide(self)
}
Kiran
  • 617
  • 6
  • 20
1

To implement simple show/hide functionality for the red (x) button, make your App Delegate class the window delegate for your main window, as well.

Then add the following code to it:

- (BOOL)windowShouldClose:(id)sender {
    [[NSApplication sharedApplication] hide:self];

    return NO;
}

- (void)applicationDidBecomeActive:(NSNotification *)notification {
    [[NSApplication sharedApplication] unhide:self];
}
0

my 2 cernts for swift 5/Xcode 10

note: You can call these methods also in ViewController (if useful) to prevent splitting code between NASWindow/NSView-Controllers. in this case:

class ViewController: NSViewController, **NSWindowDelegate**
{

...

override func viewWillAppear() {
    // we choose to delegate ourselves. must be done here, in viewDidLoad window is nil
    let window = self.view.window
    window!.delegate = self
}

...

no neeed to pass self.. : (as per other guys above.)

   func windowShouldClose(_ sender: NSWindow) -> Bool {

        NSApplication.shared.hide(nil)

        return false // prevent closing.
    }


    func applicationDidBecomeActive(_ notification: Notification) {
        NSApplication.shared.unhide(nil)
    }
ingconti
  • 10,876
  • 3
  • 61
  • 48
0

Late answer but here is what I do.

First of all you should make a class for your main window, this way you won't have to implement windowShouldClose for every controller.

class BaseWindowController: NSWindowController, NSWindowDelegate {

    override func windowDidLoad() {
        super.windowDidLoad()
        contentViewController?.view.window?.delegate = self
    }
    
    func windowShouldClose(_ sender: NSWindow) -> Bool {
        NSApplication.shared.hide(nil)
        return false
    }
}

Then in Appdelegate you should implement applicationDidBecomeActive like this

func applicationDidBecomeActive(_ notification: Notification) {
     NSApplication.shared.unhide(nil)
}

This is it now you will always can reopen your window.

mike vorisis
  • 2,786
  • 6
  • 40
  • 74