Cocoa switching a focus to my application - objective-c

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.

Related

How to show window after pressing X

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.

setActivationPolicy to Regular and openWindow

On applicationDidFinishLaunching application activationPolicy is set to Prohibited (to hide the dock icon).
I have a method that sets it to NSApplicationActivationPolicyRegular (to be able to open and focus the window) then opens the window, but I have to run it twice to work. The set policy doesn't affect the openWindow: method.
- (void)openWindow {
[NSApp activateIgnoringOtherApps:YES];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp openWindow:window];
}
Do I need to delay the openWindow:?
When do you call openWindow? Maybe you should place the calling after the applicationDidFinishLaunching.
By the way, if you want to hide the dock icon and be able to focus on the window, you should try NSApplicationActivationPolicyAccessory other than NSApplicationActivationPolicyProhibited.

Cocoa: Close the app when a HUD panel is closed

I need to close the entire app when the user click on a Panel close button,
I tried:
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
return YES;
}
but it closes the app also from file-selector panels.
Thank you in advance.
L.
Applications can't be closed, only quit. Only windows can be closed, not applications.
If you want to quit the application when only a specific window is closed, be that window's delegate and respond to the windowWillClose: message by telling the application to terminate.

Closing Mac application (clicking red cross on top) and reopening by clicking dock icon

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

Redrawing the cocoa app's main window when clicked

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;
}