macOS:有什么方法可以知道用户何时尝试通过其Dock图标退出应用程序? [英] macOS: Is there any way to know when the user has tried to quit an application via its Dock icon?

查看:151
本文介绍了macOS:有什么方法可以知道用户何时尝试通过其Dock图标退出应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可可应用程序是否有任何方法可以检测到用户何时尝试通过其Dock菜单而不是其他方法来退出该应用程序?

Is there any way for a Cocoa application to detect when the user has tried to quit it via its Dock menu, and not by some other method?

通常,可以使用应用程序委托的applicationShouldTerminate:方法捕获并响应退出事件.但是,此方法似乎无法区分来自应用程序主菜单,其Dock图标,来自Apple事件的退出请求,还是来自任何其他退出应用程序的常规方法.我很好奇是否有任何方法可以准确地知道用户如何尝试退出该应用程序.

Normally it's possible to catch and respond to quit events using the application delegate's applicationShouldTerminate: method. However, this method doesn't seem to distinguish between the request to quit coming from the application's main menu, from its Dock icon, from an Apple event, or any other conventional method of quitting the application. I'm curious if there's any way to know precisely how the user has tried to quit the application.

推荐答案

实际上,应用可以通过检查是否正在处理当前的AppleEvent来了解其退出的原因,如果可以,请检查是否查看这是否是退出事件,以及发送该事件的是Dock. (请参阅此线程,讨论如何判断应用程序是否由于系统正在退出而退出.注销或关闭.)

It is in fact possible for an app to know the reason why it's quitting by checking to see if there is current AppleEvent being handled and, if so, checking to see whether it's a quit event and whether it was the Dock that sent it. (See this thread discussing how to tell if an app is being quit because the system is logging out or shutting down.)

以下是方法的示例,当通过应用程序委托的applicationShouldTerminate:方法调用该方法时,如果通过Dock退出应用程序,则该方法将返回true:

Here is an example of a method that, when called from the application delegate's applicationShouldTerminate: method, will return true if the app is being quit via the Dock:

- (bool)isAppQuittingViaDock {
    NSAppleEventDescriptor *appleEvent = [[NSAppleEventManager sharedAppleEventManager] currentAppleEvent];

    if (!appleEvent) {
        // No Apple event, so the app is not being quit by the Dock.
        return false;
    }

    if ([appleEvent eventClass] != kCoreEventClass || [appleEvent eventID] != kAEQuitApplication) {
        // Not a 'quit' event
        return false;
    }

    NSAppleEventDescriptor *reason = [appleEvent attributeDescriptorForKeyword:kAEQuitReason];  

    if (reason) {
        // If there is a reason for this 'quit' Apple event (such as the current user is logging out)
        // then it didn't occur because the user quit the app through the Dock.
        return false;
    }

    pid_t senderPID = [[appleEvent attributeDescriptorForKeyword:keySenderPIDAttr] int32Value];

    if (senderPID == 0) {
        return false;
    }

    NSRunningApplication *sender = [NSRunningApplication runningApplicationWithProcessIdentifier:senderPID];

    if (!sender) {
        return false;
    }

    return [@"com.apple.dock" isEqualToString:[sender bundleIdentifier]];
}

这篇关于macOS:有什么方法可以知道用户何时尝试通过其Dock图标退出应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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