When run my app(OSX, macOS SDK 10.15), it shows the main window, as expected. I then CMD-Tab to another app, but when I CMD-Tab back to my app, it won't show the window. The same happens if I click on it in the dock.
I've tried various suggestions, such as:
[NSApp activateIgnoringOtherApps:YES];
[window makeKeyAndOrderFront:nil];
[window orderFrontRegardless];
and
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:#selector(applicationSwitchedTo) name:NSWorkspaceDidActivateApplicationNotification object:nil];
-(void)applicationSwitchedTo
{
[window makeKeyAndOrderFront:nil];
}
I've set up all the callback methods in both the window and app delegates, but I don't get anything,except the above callback, when I switch to or from my app. I would think it would default behavior to show the apps main window when switched to it from another app. Any help is greatly appreciated!
I figured out a fix, but I'm not sure if it's the proper thing to do.
I noticed that if I called
[NSApp finishLaunching]
then the window ordering functions don't work. I don't need it's functionality, so I removed it.
Furthermore, I'm now handling the event type NSEventTypeAppKitDefined, which is called when you switch on and off the app. I then check the subtype and call the window ordering function:
case NSEventTypeAppKitDefined:
{
if([theEvent subtype] == NSEventSubtypeApplicationActivated) {
[window makeKeyAndOrderFront:nil];
}
}
Hope this helps someone down the line!
Related
When I launch a cocoa app from terminal, the window generated by the cocoa app stays behind the terminal. Is there a way to keep the window on top?
I tried in the cocoa app :
-(void) awakeFromNib {
[self.window makeKeyAndOrderFront: self];
[self.window setLevel: NSMainMenuWindowLevel];
}
also tried with setLevel: NSFloatingWindowLevel. Neither works.
or Should I put the [self.window setLevel: NSFloatingWindowLevel] somewhere else?
Anyone can give some suggestions? Thanks.
LJ
You need to make your window the key window and then it would come on top of other windows.
[NSApp activateIgnoringOtherApps:YES];
[self.window makeKeyAndOrderFront:self]
And the correct method is awakeFromNib not awakerFromNib. Ignore if you have declared awaker method.
I've been trying to submit an application for the Apple App store and the reviewer claims that my initial notice window (which is modal) does not go away when they hit the accept button. Now I've tested it on several machines (even clean 10.7.1 installations) and have not seen this behavior (it works great for me).
My code to display this window:
- (IBAction) doAlert:(id)sender {
if(self.alertVC == nil) {
self.alertVC = [[[AlertVC alloc] initWithWindowNibName:#"AlertVC"] autorelease];
}
[NSApp runModalForWindow:self.alertVC.window];
}
and the code with which I close it looks like this:
-(IBAction)closeWindow:(id)sender {
[self close];
[NSApp stopModal];
}
Anyone have any idea why this works 100% in all my tests but for the Apple testers the window does not go away? (It stays open in the foreground even though the application continues and they can interact with the application as normal again.
The only thing I see, that may cause the error is, that you are not closing the window, but the controller (or who else provides the method closeWindow)
If closeWindow is a method of the same class than doAlert the code should look like:
-(IBAction)closeWindow:(id)sender {
[self.alertVC.window performClose:self];
[NSApp stopModal];
}
I know the answer is late and you probably have found a solution, but I provided the answer for all those stumbling over the snippet and wondering, why it happens to them as well.
I have an NSStatusItem that is properly displaying in the MenuBar. One of the items (when clicked) displays a modal NSWindow from my application, which is meant to perform a one-off task, then disappear. (Eg. the user enters a small bit of text, clicks "Save", and the modal NSWindow goes away.)
The issue occurs when the application is running in the background. The modal window properly appears above whatever application is running in the foreground, but when the user clicks the "Save" button, the rest of the application's windows also are made active. This is undesirable, as the user then has to click back to whatever app they were using. (Destroying the convenience of the NSStatusItem.) I'm displaying the modal window using:
[myWindow setFrame:finalRect display:YES animate:NO];
[myWindow setLevel:NSPopUpMenuWindowLevel];
[NSApp runModalForWindow:myWindow];
Is there any way to prevent clicks/events in my popup window from causing the rest of the application to become active? Or a way to let NSApp know that this particular panel shouldn't automatically activate the rest of the app? Thanks!
Instead of creating an NSWindow, create an NSPanel with the style NSNonactivatingPanelMask. You can then do the usual makeKeyAndOrderFront: and orderOut: to show/hide panel as needed.
NSApp's beginModalSessionForWindow, runModalSession, endModalSession are methods you need.
Have a look here for example how to use it:
Creating a fully customized NSAlert
A solution by Ken Thomases on the cocoa-dev list a couple years ago looks applicable here too:
[[NSApplication sharedApplication] hide:self];
[[NSApplication sharedApplication] performSelector:#selector(unhideWithoutActivation)
withObject:nil
afterDelay:0.05];
Which in theory tells the application to hide itself and unhide at the bottom of the window stack.
You could also intercept the mouse click event and use [NSApp preventWindowOrdering]
You can try something like:
...
if ([NSApp isHidden])
[myWindow makeKeyAndOrderFront:self];
else
[NSApp runModalForWindow:myWindow];
...
and when finish:
...
if ([NSApp isHidden])
[myWindow orderOut:self];
else
[NSApp stopModal];
...
At the bottom of this post I included an example project that has my code in it. I have a Cocoa Application that uses a main default window, and another window nib I created. When the program starts, I want it to load the window nib and show it in front of the default 'MainMenu' nib window. How can I do this? I've tried the following code, but the window is still displayed behind the default window:
InfoWindowController *winInfo = [InfoWindowController new];
[winInfo.window makeKeyAndOrderFront:self];
[winInfo.window setOrderedIndex:0];
[winInfo.window makeKeyAndOrderFront:self];
[winInfo showWindow:self];
This is being called from AppController, which is a class I added to the MainMenu nib. InfoWindowController subclasses NSWindowController. I have included an example project here.
So, I know i'm reviving an old thread, but I was having a similar issue. Try putting [NSApp activateIgnoringOtherApps:YES]; in there.
I'm running into a few problem with a background application that uses LSUIElement=1 to hide its dock item, menu bar and prevent it from appearing in the Command-Tab application switcher.
It seems to be a Snow Leopard only problem.
The application places an NSStatusItem in the menu bar and pops up a menu when clicked on. Selecting "Preferences..." should bring up an NSWindow with the preferences.
The first thing that doesn't seem to work is that the Window does not get ordered in at the front, but appears behind all other application windows.
I tried to fix this by calling
[[NSApplication sharedApplication] activateIgnoringOtherApps: YES]
but that didn't work.
After a while I figured out that the menu is blocking the message to the run loop from being sent, so I wrote another method on the MainController and sent the message with a delay:
[self performSelector:#selector(setFront:)
withObject: [preferencesController window] afterDelay:1.0];
-(void)setFront: (id) theWindow {
[[NSApplication sharedApplication]activateIgnoringOtherApps:YES];
[theWindow orderFrontRegardless];
[theWindow makeKeyWindow];
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}
Note the send-every-possible-message-to-make-it-do-what-it-should-be-doing-approach.
This works, kind-of, the window is brought to the front on top of all other windows from all apps, BUT most of the time it isn't active, meaning it's title bar is greyed out. Clicking on the title bar won't make the window active either. Clicking INSIDE of the window will make it active!?
This all didn't seem to be a problem in Leopard; just calling activateIgnoringOtherApps and making the window key seemed to work just fine.
In Snow Leopard there is a new API designed to replace LSUIElement that is supposed to emulate its behaviour:
http://developer.apple.com/mac/library/releasenotes/cocoa/appkit.html
I've played around with that, but it's SL-only and I haven't been able to get LSUIElement being set.
That's strange - I'm writing a LSUIElement application under Snow Leopard, and I didn't have such problems as you've described... I did have the problem that the newly created window didn't appear at the front, but I fixed it by calling activateIgnoringOtherApps. This was all I had to do to make it work as it should:
[NSApp activateIgnoringOtherApps: YES];
[preferencesWindow makeKeyAndOrderFront: self];
I didn't even touch anything that had 'policy' in the name.
After posting the question in desperation, I did continue looking and did eventually find the solution. Since this stumped me for a few days and there seems to be no other answer out there that google can find, I'll explain the solution for "future generations".
Snow Leopard adds a new NSApplication presentationOptions API:
http://developer.apple.com/mac/library/releasenotes/cocoa/appkit.html
This is supposed to simulate the way that LSUIElement works, but provide more developer control. Unfortunately, the simulation isn't perfect so there is a change of behaviour between 10.5 and 10.6.
In particular, if your application has the LSUIElement = 1 line in its info.plist, Snow Leopard will initialize "the application’s presentationOptions .. to an equivalent combination of NSApplicationPresentationOptions flags instead".
Only it doesn't really. It sets the new NSApplication setActivationPolicy to NSApplicationActivationPolicyAccessory:
"The application does not appear in the Dock and does not have a menu bar, but it may be activated programmatically or by clicking on one of its windows. This corresponds to value of the LSUIElement key in the application’s Info.plist being 1."
Despite the mention of being activated programatically, activateIgnoringOtherApps: is simply ignored completely.
The solution is to set the activation policy to "regular":
[[NSApplication sharedApplication] setActivationPolicy: NSApplicationActivationPolicyRegular];
Of course, you can only do this if you use the 10.6 SDK as Base SDK, something few people want to do at the moment, so below is a 10.5-safe way of doing this:
NSApplication* app = [NSApplication sharedApplication];
if( [app respondsToSelector: #selector(setActivationPolicy:)] ) {
NSMethodSignature* method = [[app class] instanceMethodSignatureForSelector: #selector(setActivationPolicy:)];
NSInvocation* invocation = [NSInvocation invocationWithMethodSignature: method];
[invocation setTarget: app];
[invocation setSelector: #selector(setActivationPolicy:)];
NSInteger myNSApplicationActivationPolicyAccessory = 0;
[invocation setArgument: &myNSApplicationActivationPolicyAccessory atIndex: 2];
[invocation invoke];
}
I hope somebody will find this useful.