I'm writing a Mac OS X application using Xcode 6 and Objective C.
After starting the application and press the X, the window will be hidden. Is there a way to show up the window with clicking on the App-Symbol on the dock (like in Safari)?
On clicking "X" or close dot then window closes and if it's a root Window then your app will be closed. So if you want to hide on clicking "X" or close dot then you could use below:-
//Called when clicked close option on window
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSApplication *) theApplication
{
[[NSApplication sharedApplication] hide:self];
return NO;
}
//Called when you tap app icon on dock.
-(BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag{
[[self.mainWindowController window] makeKeyAndOrderFront:self];
return YES;
}
But remember when we hide a OS X app then after a certain time the OS will sleep that app ,called NSAppNap will take place and your background work will be delayed or simply OS will perform those operation in a batching form.
When the user clicks the "X" (or red "Close" dot), that actually fully closes the window and does not hide it.
To make the window reappear (assuming it's still in memory and not released thanks to ARC), you need to do "showWindow" on the window controller. And when clicking on the dock icon, the best place to catch the dock icon being clicked might be NSApplicationDelegate's "applicationDidBecomeActive:" notification.
Related
I am on objectiveC, OSX, not iOS. XCode 8.3
I have a preferences Window (custom NSWindow) that opens as a modal on my main window.
The preferences window itself contains a view with tabs. The tab height changes the windows size whenever one is clicked.
First Tab clicked:
Second Tab clicked:
Now if someone hides the application in the dock and activates it again, the preferences window becomes active with the height of tab 1, even if tab 2 is still active. So the content gets cut off.
What i need is some kind of notification that gets triggered on becoming active/visible again to trigger a resize of the window before it gets displayed.
I tried it with these notifications in my NSWindow subclass (with NSWindow delegate set).
- (void)windowDidResignMain:(NSNotification*)notification{
NSLog(#"windowDidResignMain");
}
- (void)windowDidResignKey:(NSNotification*)notification{
NSLog(#"windowDidResignKey");
}
- (BOOL)canBecomeKeyWindow{
return YES;
}
- (BOOL)canBecomeMainWindow{
return YES;
}
But none of them worked. Is it because it's a modal window?
Any help appreciated.
I found it. My mistake - my tabViewController triggered a resize on viewWillAppear with always the first tab. I changed that to the current selected tab and that was it.
I'm running a modal window in my application, which works fine. The problem however seems to be dismissing the window.
While the window closes, it takes at least 20 seconds for the application to continue (I show the modal window at startup), but it will continue instantly if I click on its dock icon.
Here's how I close the modal window:
[NSApp stopModal];
[updaterWindow orderOut:self];
[updaterWindow close];
I've tried various things to get the Application to continue faster, such as manually switching to the main run loop and speaking to the main window directly (asking it to become the key window), which all did not work. Same as before, 20 seconds wait or instant load when clicking on the dock icon.
What can cause something like this? I'm really baffled.
Edit:
My modal window is actually created in a really simple way. I run my window as modal:
[NSApp runModalForWindow:updaterWindow];
When I am done with things, I close it:
[NSApp stopModal];
[updaterWindow orderOut:self];
[updaterWindow close];
The window goes away, but it needs a click to the dock icon or anywhere on the screen for the app to continue.
I'm doing something similar in my app at the moment: I have an IBAction method that calls the modal window:
-(IBAction)showMyModalWindow:(id)sender {
[theModalWindow makeKeyWindow];
NSInteger retVal = [NSApp runModalForWindow:theModalWindow];
[theModalWindow close];
//continue and do somethin according the value in retVal
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}
In this case theModalWindow is in it's own nib file has an NSObject in it that deals with the ok and cancel button to dismiss theWindow, by way of example here's the cancel button callback:
-(IBAction)cancelButton:(id)sender
{
[NSApp stopModalWithCode:errAuthorizationCanceled];
}
The last line in showMyModalWindow: method seems to be what's needed in my case by sending the app forward... Hope that's of some use!
Todd.
When I close my Mac application (by clicking red cross button on window top bar) the app icon stays in the dock at the bottom. Now this is normal behaviour. When user click on it again it does not fire up the application unless the user quits the application altogether and relaunches it again.
A similar example on Mac OS X is "Activity Monitor". You can close the application by clicking the red cross button at the top the but dock icon stays there. User can re-open it by clicking dock icon.
How can I achieve this in my own application ?
If you are still concerned how to reopen the window that you have closed, use this method:
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag {
[window makeKeyAndOrderFront:self];
return YES;
}
You can use this to handle clicks on the applications icon in the dock.
For further information check out the NSApplicationDelegate Protocol Reference.
Here is the documentation:
http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSApplicationDelegate_Protocol/Reference/Reference.html
Hope this helps!
Latest Update:
In latest 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
}
Implement the method
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
return NO;
}
in your app delegate
Your app will hang around after the window is closed and then if you implement
- (void)applicationDidBecomeActive:(NSNotification *)aNotification
{
//dock icon has just been clicked , or cmd-tabbed into
}
in the app delegate
You can do things when the icon is clicked such as open a new or old window if you need to
See http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSApplicationDelegate_Protocol/Reference/Reference.html for other relevant application events
I think that the answers above aren't fully correct, to achieve this you should override applicationShouldHandleReopen(_:hasVisibleWindows:) https://developer.apple.com/reference/appkit/nsapplicationdelegate/1428638-applicationshouldhandlereopen
I made an Mac OS X cocoa app, and when I click the red button the main window disappears.
However, when I clicked the icon in dock, it doesn't show the main window anymore.
What's wrong? How do i redraw the main window by catching what message?
You might be able to do something like this in your application delegate:
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender {
//show your window here
return NO;
}
I have a statusbar application (the one that runs in the top bar of the system). When i click on the icon in the statusbar, a menu pops up and it has an item to configure my app. When i press this item, the main app window is shown [wndMain makeKeyAndOrderFront:self]; The problem is clicking a statusbar icon doesn't activate the application and, if other application is active at that moment, the main window will open below that active application and will be out of focus. How can i programatically switch focus to my application when clicking on the statusbar icon so that the settings window would always open on top of other windows and would be focused and ready for user input?
Thank you
Send the shared NSApplication object an activateIgnoringOtherApps: message.
NSApplication *myApp = [NSApplication sharedApplication];
[myApp activateIgnoringOtherApps:YES];
[self.window orderFrontRegardless];
you can use the code above to solve the problem.