Jan Aagaard
Jan Aagaard

Reputation: 11194

OS X: Show interface of application agent (UIElement)

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:

Upvotes: 4

Views: 1463

Answers (2)

Jan Aagaard
Jan Aagaard

Reputation: 11194

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

Upvotes: 2

VenoMKO
VenoMKO

Reputation: 3294

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

Upvotes: 2

Related Questions