使用cocoa检测挂起的系统关闭

时间:2012-03-22 11:14:24

标签: macos cocoa xcode4 osx-snow-leopard shutdown

如何检测计算机即将关闭可可?互联网上似乎没有任何东西。这必须区分关闭和注销。

有人可以帮我吗?

2 个答案:

答案 0 :(得分:4)

与Matthias提供的代码相同,但将通知的名称更改为:

<强> NSWorkspaceWillPowerOffNotification

如果您想阻止系统关机,请添加

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

确保使用&#34; NSApplicationDelegate&#34;

祝你好运!

答案 1 :(得分:1)

官方文档中的以下代码可能对您有所帮助:

 - (void) receiveSleepNote: (NSNotification*) note
 {
     NSLog(@"receiveSleepNote: %@", [note name]);
 }

 - (void) receiveWakeNote: (NSNotification*) note
 {
     NSLog(@"receiveSleepNote: %@", [note name]);
 }

 - (void) fileNotifications
 {
     //These notifications are filed on NSWorkspace's notification center, not the default 
     // notification center. You will not receive sleep/wake notifications if you file 
     //with the default notification center.
     [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self 
        selector: @selector(receiveSleepNote:) 
        name: NSWorkspaceWillSleepNotification object: NULL];

     [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self 
        selector: @selector(receiveWakeNote:) 
        name: NSWorkspaceDidWakeNotification object: NULL];
 }

有关详细信息,请参阅:https://developer.apple.com/library/mac/#qa/qa1340/_index.html