OSX:注销时正确退出应用程序

时间:2017-09-06 10:44:02

标签: objective-c macos

每当我尝试注销当前用户时(不是快速用户切换!)我从macOS收到一条消息(对不起,如果不完全是消息,我用德语获取):" Unable注销,因为应用程序" com.my.app"不退出"。

为了避免这种情况,我需要什么?我目前在我的AppDelegate中有这个:

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
    return NSTerminateNow;
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
    return true;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self
                                                           selector: @selector(receiveLogOffOrShutdown:)
                                                               name: NSWorkspaceWillPowerOffNotification object: NULL];
}

-(void) receiveLogOffOrShutdown: (NSNotification*) note
{
    [application stop:nil];
}

我所观察到的例子是receiveLogOffOrShutdown永远不会被触发。和applicationShouldTerminate一样,永远不会触发断点。

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:0)

使用NSApp.terminate方法

目标C

-(void)relaunch {
    int processIdentifier = NSProcessInfo.processInfo.processIdentifier;
    NSString *path = [NSBundle mainBundle].executablePath;
    NSArray *arg = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%d",processIdentifier], nil];
    [NSTask launchedTaskWithLaunchPath:path arguments:arg];
    [NSApp terminate:self];
}

<强>夫特

func relaunch() {
        let processIdentifier: Int32 = NSProcessInfo.processInfo().processIdentifier
        let path = NSBundle.mainBundle().executablePath! as NSString
//        let myPath: String = "\(path.fileSystemRepresentation)"
        NSTask.launchedTaskWithLaunchPath(path as String, arguments: ["\(processIdentifier)"])
        NSApp.terminate(self)  
    }

func terminate(sender:AnyObject?) 调用时,此方法执行几个步骤来处理终止请求

希望这对你有所帮助。

相关问题