OS X 登录助手应用认为应用已经 运行

OS X Login helper app thinks app is already running

我正在努力实现一个辅助应用程序来启动主应用程序,

我想确保,如果登录时已经有一个应用程序 运行 的实例,助手应用程序不会启动该应用程序的第二个实例,并适当地自行终止。

当我对此进行测试并查看控制台的输出时,我确实看到我的助手应用程序已经认为有一个应用程序实例 运行,即使没有。因此,辅助应用程序将在不启动主应用程序的情况下退出。有没有人知道为什么助手应用程序可能认为存在现有应用程序实例,即使没有?

#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

runningApplicationsWithBundleIdentifier: 不会 return nil,它会 return 一个空数组,所以这个比较总是评估为 YES。
引自 docs:

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