Login Helper App creating multiple OS X app instances

366 Views Asked by At

I'm using the ServiceManagement framework to add a login item that will launch a Helper App, which will launch the main app whenever the user logs back in. The addLoginItem and disableLoginItem methods are called if the user selects or deselects "Launch at Login" with an NSButton of a switch-type:

//Add the Helper app as a login item
- (void)addLoginItem
{
    NSLog(@"Enable login item");
    if (!SMLoginItemSetEnabled((__bridge CFStringRef)kLoginHelperBundleIdentifier, true)) {
    }
}

//Disable the Helper app as a login item
- (void)disableLoginItem
{
    NSLog(@"Disable login item");
    if (!SMLoginItemSetEnabled((__bridge CFStringRef)kLoginHelperBundleIdentifier, false)) {
    }
}

The code for the helper app is rather simple...

- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
    [[NSWorkspace sharedWorkspace] launchApplication: @"My App"];

    [[NSApplication sharedApplication] terminate:self];
}

The problem is that when the Main App is running, if a user repeatedly selects and deflects the 'Launch At Login' button, a second instance of the Main App will be launched. Whereas, what I want to happen is that the Main App will only be launched upon the user logging back in.

Upon looking at SMLoginItem.h, I saw that the documentation stated the following:

 * @param enabled
 * The Boolean enabled state of the helper application. This value is effective
 * only for the currently logged in user. If true, the helper application will
 * be started immediately (and upon subsequent logins) and kept running. If
 * false, the helper application will no longer be kept running.

So, it seems as if the Helper App is being launched every time addLoginItem is called. Knowing that, I modified my Helper App to check if my main app is already running. If it is, then we will terminate the Helper App. Otherwise, we'll launch an instance of the Main App, and then terminate the Helper App.

- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
    if ([NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.myCompany.MyApp"]){

        [[NSApplication sharedApplication] terminate:self];

    }

    else {
        [[NSWorkspace sharedWorkspace] launchApplication: @"My App"];

        [[NSApplication sharedApplication] terminate:self];

    }
}

However, if the user selects and deselects the "Launch At Login" button repeatedly, then a second instance of the Main App will be created. Does anyone have any idea on how to make sure a second instance of the Main App is not created by the Helper App if the user toggles "Launch At Login" multiple times?

EDIT The app is not being distributed through the Mac App Store, which is why I do not have code-signing or sandboxing enabled per the instruction in this tutorial.

1

There are 1 best solutions below

0
On BEST ANSWER

The problem being that I had a few different copies of the application on my system (specifically, one on the desktop and on in the applications folder). Removing one of these application instances resolved my issue.