2012-03-22 57 views
1

如何檢測計算機何時關閉可可?互聯網上似乎沒有任何東西。這必須區分關機和註銷。用可可檢測待處理系統關機

有人可以幫我嗎?

+0

如果其中一位確實幫助解決了問題,請接受答案。這兩個答案結合在一起解決了你的問題。 – 2015-07-07 23:22:10

回答

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

+0

感謝您的回答。不是我想要的。不告訴我如何檢測關機。我會搜索開發人員。 – yskywalker 2012-03-25 15:56:45

4
爲馬蒂亞斯提供,但更改通知的名稱爲

同一代碼:

NSWorkspaceWillPowerOffNotification

如果你要防止系統停機,請加

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

務必使用「NSApplicationDelegate」

好運!