OS X Login helper app thinks app is already running

163 views Asked by At

I'm working on implementing a helper app to launch a main, non-sandboxed app upon user login.

I want to make sure that, in the event there is already an instance of the app running at login, the helper app does not launch a second instance of the app, and terminates itself appropriately.

When I'm testing this, and looking at Console's output, I do see that my helper app already thinks there is an app instance running, even if there is not. So, the helper app will quit without launching the main app. Does anyone have an idea on why the helper app may think there is an existing app instance, even though there is not?

#import "AppDelegate.h"


@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
    //Check if we're currently running My App. If we are, just quit the helper app.
    if ([NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.me.myApp"]) {
        NSLog(@"We're running My App already, so we're going to quit.");
    }

    //Otherwise, launch My App, then quit the helper app.
    else {
        [[NSWorkspace sharedWorkspace] launchApplication:@"My App"];
    }

    [[NSApplication sharedApplication] terminate:self];
}


@end
1

There are 1 answers

2
Sega-Zero On BEST ANSWER

runningApplicationsWithBundleIdentifier: will not return nil, it will return an empty array, so this comparison always evaluates to YES.
quote from the docs:

Return Value
An array of NSRunningApplications, or an empty array if no applications match the bundle identifier.