macos - 使用 NSAppleEventManager 捕获所有退出事件

标签 macos events

当我的 Mac OS 应用程序退出时,我希望它询问用户“您确定要退出 [Yes] [No]”。

我试过这个:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    // Install a custom quit event handler
    NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager];
    [appleEventManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication];
}

// Handler for the quit apple event
- (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent {
    // Insert YES/NO-dialog here
    // if(blahahaha..
    //[NSApp terminate:self];
}

但它只会捕捉到右键单击扩展坞上的应用程序然后选择“退出”时出现的退出。如果我按 Cmd-Q 或从应用程序菜单中选择退出,我的处理程序不会被调用...

最佳答案

并非所有这些案例都涉及 Apple 事件。相反,处理应用程序委托(delegate)方法-applicationShouldTerminate: .

关于macos - 使用 NSAppleEventManager 捕获所有退出事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8620146/

相关文章:

macos - Notational Velocity 或 nvAlt 的网络替代品

android - 无法在OSX中使用gradle--retrolambda构建项目

macos - 使用 AppleScript 设置窗口位置

javascript - 如何使用 jQuery/Javascript 将事件从一个选项卡/窗口发送到另一个选项卡/窗口

amazon-web-services - 可扩展的事件调度程序,可容纳数十万个事件

c - Mac使用gcc运行c程序

php - Laravel:当用户表中的特定列发生变化时如何触发事件?

C# 从控件列表引发事件

jquery - jQuery绑定(bind)事件上的RequireJS不会触发方法

macos - 检测 OSX 上多设备应用程序的空闲时间