【发布时间】:2014-10-29 16:00:42
【问题描述】:
我有一个沙盒菜单栏应用程序(没有停靠图标),在它的首选项窗口中允许用户选中复选框以在登录时启动应用程序。我曾经使用 LSSharedFileList api,但由于沙盒应用程序不再允许这样做,我已经迁移到使用 SMLoginItemSetEnabled。我发现,虽然应用程序将在登录时启动,如预期的那样,如果我回到首选项并取消选中并重新选中登录时启动复选框,我会启动我的菜单栏应用程序的第二个实例。
这是我的辅助应用代码(在其应用委托中):
- (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!");
}
}
第二个实例启动后,取消选中/重新选中复选框不会再启动实例。有谁知道发生了什么? 谢谢
【问题讨论】: