Обнаружить ожидание выключения системы с какао - PullRequest
1 голос
/ 22 марта 2012

Как я могу определить, когда компьютер собирается выключиться с какао? Кажется, в интернете ничего нет. Это должно различать выключение и выход из системы.

Кто-нибудь может мне помочь с этим?

Ответы [ 2 ]

4 голосов
/ 15 августа 2012

Тот же код, что и у Матиаса, но измените имя уведомления на:

NSWorkspaceWillPowerOffNotification

И если вы хотите предотвратить отключение системы, добавьте

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

обязательно используйте "NSApplicationDelegate"

Удачи!

1 голос
/ 22 марта 2012

Вам может помочь следующий код из официальной документации:

 - (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

...