使用目标C在MAC上捕获注销(未关闭电源)事件 [英] Catching Logoff (not power off) event on MAC using objective C

查看:124
本文介绍了使用目标C在MAC上捕获注销(未关闭电源)事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何捕获用户注销事件吗?我可以使用NSWorkspaceWillPowerOffNotification通知捕获电源关闭事件.但这并没有显示注销和关机事件的任何区别.我想分别注销和关闭电源.我能够在Windows上捕捉到同样的东西.

Can anybody tell me how can I catch user logoff event? I am able to catch poweroff event using NSWorkspaceWillPowerOffNotification notification. But it is not showing any difference in logoff and power off events. I want to catch log off and power off separately. I am able to catch the same on windows.

推荐答案

首先,您必须确保您的应用未参与

First, you have to make sure your app is not participating in sudden termination. If it is, then it can be killed at logout without any opportunity to react.

现在,当用户注销时,您的应用程序将获得一个kAEQuitApplication('quit')Apple Event.这与-[NSApplication terminate:]的调用具有相同的效果.您可以实现应用程序委托方法-applicationShouldTerminate:,以通知其退出请求并控制应用程序对它的响应.

Now, when the user logs out, your app will get a kAEQuitApplication ('quit') Apple Event. This will have the same effect as an invocation of -[NSApplication terminate:]. You can implement the application delegate method -applicationShouldTerminate: to be notified of the request to quit and control your app's response to it.

在该委托方法中,您可以使用如下代码检查退出事件并了解退出请求的原因:

In that delegate method, you can use code like this to examine the quit event and learn the reason for the quit request:

    NSAppleEventManager* m = [NSAppleEventManager sharedAppleEventManager];
    NSAppleEventDescriptor* desc = [m currentAppleEvent];

    switch ([[desc attributeDescriptorForKeyword:kAEQuitReason] int32Value])
    {
        case kAELogOut:
        case kAEReallyLogOut:
            // log out
            break;
        case kAEShowRestartDialog:
        case kAERestart:
            // system restart
            break;
        case kAEShowShutdownDialog:
        case kAEShutDown:
            // system shutdown
            break;
        default:
            // ordinary quit
            break;
    }

这篇关于使用目标C在MAC上捕获注销(未关闭电源)事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆