推出我有一个沙盒菜单栏的应用程序(不停靠图标),在它的偏好设置窗口允许用户检查复选框,让应用程序启动时登录。我曾经使用LSSharedFileList api,但由于沙盒应用程序不允许这样做,所以我已经迁移到使用SMLoginItemSetEnabled。我发现,虽然应用程序将按照预期启动,但如果我回到首选项并取消选中并重新检查登录启动复选框,我会启动第二个菜单栏应用程序实例。设置MacOS X的菜单栏的应用程序在启动时
这里是我的助手应用程序代码(在其应用程序委托):
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSString * path = [[[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent]
stringByDeletingLastPathComponent]
stringByDeletingLastPathComponent]
stringByDeletingLastPathComponent];
[[NSWorkspace sharedWorkspace] launchApplication:path];
[NSApp terminate:nil];
}
这里是我的偏好设置窗口(主应用程序)代码:
- (IBAction)toggleLoginStatus:(NSButton*)sender{
if(!SMLoginItemSetEnabled((__bridge CFStringRef)@"myAppBundleIdentifier", (BOOL)[sender state])){
NSLog(@"Dagnabit!");
}
}
第二个实例启动后,取消选中/重新选中该复选框不会再启动实例。有谁知道发生了什么事? 谢谢
废话!看起来问题比最初出现的要深一点。在运行Yosemite的新款MB Pro 15上,在登录时取消选中/检查启动会导致启动第二个实例,而在运行Yosemite的旧版2008 MB Pro 15上则不启动第二个实例。我添加了代码来检查应用程序是否已在运行。在这两种情况下,该应用都会在登录时启动,但较新的版本会显示这种奇怪的行为。有人有主意吗。 – Lazloman 2014-10-30 03:06:37