2012-02-12 15 views

Respuesta

7

acaba de encontrar:

- (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(receiveWakeNote:) 
                   name: NSWorkspaceDidWakeNotification object: NULL]; 
} 
+0

dos cosas: que quieres "nulo" no "NULO" para su objeto, y se debe formatear su respuesta para mostrar su código como código formateado, es bastante ilegible en este momento. ¡Pero es un buen trabajo responder tu propia pregunta! –

+0

¿Cómo puedo formatear correctamente el código? No sé la etiqueta requerida ... ¡Gracias! –

+0

Parece que Parag te ganó. Pero en el futuro, mira los botones en el editor. Uno de ellos es un par de llaves ("{}"). Use esto para formatear un bloque de texto seleccionado como código. –

2

Puede utilizar IORegisterForSystemPower().

conecta la llamada al dominio IOService Raíz de energía con el fin de recibir el sueño & notificaciones vigilia para el sistema. No proporciona el cierre del sistema y reinicia las notificaciones.

io_connect_t IORegisterForSystemPower (
    void *refcon, 
    IONotificationPortRef *thePortRef, 
    IOServiceInterestCallback callback, 
    io_object_t *notifier) ; 

Tome un vistazo a Q:How can my application get notified when the computer is going to sleep or waking from sleep? How to I prevent sleep?

2

para SWIFT 3:

func onWakeNote(note: NSNotification) { 
    print("Received wake note: \(note.name)") 
} 

func onSleepNote(note: NSNotification) { 
    print("Received sleep note: \(note.name)") 
} 

func fileNotifications() { 
    NSWorkspace.shared().notificationCenter.addObserver(
     self, selector: #selector(onWakeNote(note:)), 
     name: Notification.Name.NSWorkspaceDidWake, object: nil) 

    NSWorkspace.shared().notificationCenter.addObserver(
     self, selector: #selector(onSleepNote(note:)), 
     name: Notification.Name.NSWorkspaceWillSleep, object: nil) 
} 
Cuestiones relacionadas