Login Helper App创建多个OS X应用程序实例

时间:2015-07-16 19:54:49

标签: objective-c macos service-management

我正在使用ServiceManagement框架添加一个启动帮助应用程序的登录项,只要用户重新登录,它就会启动主应用程序。addLoginItem和{{1}如果用户使用disableLoginItem开关类型选择或取消选择“在登录时启动”,则调用方法:

NSButton

帮助应用程序的代码非常简单......

//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)) {
    }
}

问题是当主应用程序运行时,如果用户反复选择并取消“启动时登录”按钮,将启动主应用程序的第二个实例。然而,我想要发生的是主应用程序将仅在用户重新登录时启动。

在查看- (void)applicationDidFinishLaunching:(NSNotification *)notification { [[NSWorkspace sharedWorkspace] launchApplication: @"My App"]; [[NSApplication sharedApplication] terminate:self]; } 后,我看到文档中说明了以下内容:

SMLoginItem.h

因此,似乎每次调用 * @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. 时都会启动帮助应用程序。知道了,我修改了我的帮助应用程序以检查我的主应用程序是否已经运行。如果是,那么我们将终止帮助应用程序。否则,我们将启动主应用程序的实例,然后终止帮助应用程序。

addLoginItem

但是,如果用户反复选择并取消选择“在登录时启动”按钮,则将创建主应用程序的第二个实例。如果用户多次切换“在登录时启动”,有没有人知道如何确保辅助应用程序不创建主应用程序的第二个实例?

编辑该应用未通过Mac App Store分发,这就是我没有根据this tutorial.

中的说明启用代码签名或沙盒的原因

1 个答案:

答案 0 :(得分:0)

问题是我的系统上有一些不同的应用程序副本(特别是桌面和应用程序文件夹中的一个)。删除其中一个应用程序实例解决了我的问题。