How to activate app's running instance

Hope my scenario is not weird. Here is the description:

1. User runs my app.

2. User hides my app completely (all windows are hidden) so that he/she has no way to accessing the app. (Dont' ask why, user requested this feature and I want to implement it.)

3. Now user rerun my app and I want to use XPC to tell the running instance to show itself.


It sounds very reasonable and I have done most of code. But it is blocked by macOS the system. It seems macOS controls only one instnace of an app can be running at the same time; second try to run the same app (in /Applications) simply does nothing - the app code does not even get executed so that my code in main.m won't have the change to communicate with XPC.


I believe there must be a way to overcome this. Any suggestions will be appreciated. Thanks and Merry Xmas!

Accepted Reply

This thread has been deleted

I hide all windows completely from dock using a method like below

Just by by of terminology, this isn’t ‘hiding all windows’, this is transforming your process into a what’s commonly called a UI element, that is, a GUI that has no normal UI. UI elements can display UI, but don’t show up in the Dock, or when you hit command-Tab, and so on.

I had to test this to confirm, but it turns out that UI elements do get the

-applicationShouldHandleReopen:hasVisibleWindows:
delegate callback when the user double clicks the app in the Finder, so it seems that like that’s the best way forward.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

I would expect your running app to get app delegate callbacks for applicationDidUnhide or applicationDidBecomeActive (or both). Do you implement those?

2. User hides my app completely (all windows are hidden) so that he/she has no way to accessing the app.

Does your app show up in the Dock?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Oh, I forgot to mention this. I hide all windows completely from dock using a method like below:

+ (BOOL)hideWindowFromDock:(NSWindow *)window
{
    if ([NSApp activationPolicy] != NSApplicationActivationPolicyProhibited)
    {
        [NSApp setActivationPolicy:NSApplicationActivationPolicyProhibited];
    }
    [window setIsVisible:NO];
    return YES;
}

>User hides my app completely (all windows are hidden) so that he/she has no way to accessing the app.


Even via cmd-tab? Or Apple logo/recent items?