15

I am developing an app in Xcode on Mac and would like to know the event which is fired when the mac gets back from sleep. AwakeFromNib doesn't seem to work.

3 Answers 3

17

For 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)
}

For swift 4:

@objc func onWakeNote(note: NSNotification) {
    ...
}

@objc func onSleepNote(note: NSNotification) {
    ...
}

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

    NSWorkspace.shared.notificationCenter.addObserver(
        self, selector: #selector(onSleepNote(note:)),
        name: NSWorkspace.willSleepNotification, object: nil)
}
0
11

Just found it:

- (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];
}
3
  • Two things: you want "nil" not "NULL" for your object, and you should format your answer to display your code as formatted code - it's pretty unreadable right now. But good job answering your own question! Commented Feb 12, 2012 at 9:39
  • How can I properly format the code ? I don't know the tag required... Thanks ! Commented Feb 12, 2012 at 13:08
  • 1
    Looks like Parag beat you to it. But in the future, look at the buttons in the editor. One of them is a pair of braces ("{}"). Use this to format a selected block of text as code. Commented Feb 12, 2012 at 18:18
3

You can use IORegisterForSystemPower().

Connects the caller to the Root Power Domain IOService for the purpose of receiving sleep & wake notifications for the system. Does not provide system shutdown and restart notifications.

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

Take a look at Q:How can my application get notified when the computer is going to sleep or waking from sleep? How to I prevent sleep?

Not the answer you're looking for? Browse other questions tagged or ask your own question.