Question

How do I make the interface for an application that has 'Application is agent (UIElement)' set to yes reappear?

The interface shows up the first time I start the app, but if I close the window, and the click on the app's icon then nothing happens. I guess that it's because OS X is trying to start the app again, and there is some mechanism preventing that. What I would like is this:

  • The first click on the app's icon should launch the app and show the interface.
  • If the interface has been closed down (but the app is still running in the background) a subsequent click on the icon should just show the interface.
  • If the interface is already shown a click on the icon should simply move the window to the foreground.
Était-ce utile?

La solution 2

I found the answer here: Closing Mac application (clicking red cross on top) and reopening by clicking dock icon.

- (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication
    hasVisibleWindows:(BOOL)flag
{
    [self.window makeKeyAndOrderFront:self];
    return YES;
}

Autres conseils

Here is a way you can do it:

1) add + initialize method to your app delegate

+ (void)initialize
{
    // check if there is a running instance of your app
    NSArray * apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]];
    if ([apps count] > 1)
    {
        //post notification to it to update inteface
        [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"updateInterface" object:nil];
        //quit current instance of the app, coz you don't need two apps running continiously
        exit(0);
    }
}

2) Register your app for the notification

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInterface:) name:@"updateInterface" object:nil];
}

3) Add updateInterface method

- (void)updateInterface:(NSNotification *)aNotification
{
    // handle your interface here
    // ....

    // move your app forward
    [NSApp activateIgnoringOtherApps:YES];
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top