Cannot get NSWorkspaceDidWakeNotification in fast OSX

I am making a Mac application where I need to do something when the computer wakes up, falls asleep and wakes up, but I cannot get the listener to work. I feel like I've tried everything. In AppDelegate.swift, inside the applicationDidFinishLaunching function, I have:

NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "sleepListener", name: NSWorkspaceWillSleepNotification, object: nil)
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "wakeUpListener", name: NSWorkspaceDidWakeNotification, object: nil)

and inside AppDelegate.swift, but outside the applicationDidFinishLaunching function, I have:

func sleepListener(aNotification : NSNotification) {
    print("Sleep Listening");
}

func wakeUpListener(aNotification : NSNotification) {
    print("Wake Up Listening");
}

I tried a combination of many different things to fix this problem. I tried listening to NSNotificationCenter.defaultCenter (), I tried changing the selector to "sleepListener:" and "wakeUpListener:", I tried to remove the arguments from both functions, and so far nothing has worked. And the really interesting thing is that I have two other listeners that work great, "NSWorkspaceScreensDidSleepNotification" and "NSWorkspaceScreensDidWakeNotification", invoking them with

    NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "screenSleepListener", name: NSWorkspaceScreensDidSleepNotification, object: nil)

and

    NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "screenWakeUpListener", name: NSWorkspaceScreensDidWakeNotification, object: nil)

referring to functions

func screenSleepListener() {
    print("Screen Sleep Listening");
}

func screenWakeUpListener() {
    print("Screen Wake Up Listening");
}

, , ? -, ? - , , , , . - , , .

!

+4
2

, -.

, .

    NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "sleepListener", name: NSWorkspaceWillSleepNotification, object: nil)
    NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "wakeUpListener", name: NSWorkspaceDidWakeNotification, object: nil)
func sleepListener() {
    print("Sleep Listening");
}

func wakeUpListener() {
    print("Wake Up Listening");
}

NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "sleepListener:", name: NSWorkspaceWillSleepNotification, object: nil)
        NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "wakeUpListener:", name: NSWorkspaceDidWakeNotification, object: nil)

func sleepListener(aNotification : NSNotification) {
    print("Sleep Listening");
}

func wakeUpListener(aNotification : NSNotification) {
    print("Wake Up Listening");
}

NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "sleepListener:", name: NSWorkspaceWillSleepNotification, object: nil)

NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "sleepListener:", name: NSWorkspaceDidWakeNotification, object: nil)

 func sleepListener(aNotification : NSNotification) {
            if aNotification.name == NSWorkspaceWillSleepNotification{
                print("Going to sleep")
            }else if aNotification.name == NSWorkspaceDidWakeNotification{
                print("Woke up")
            }else{
                print("Some other event other than the first two")
            }
        }

, . , .

,

+3

4:

func applicationDidFinishLaunching(_ aNotification: Notification) {  
    NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(sleepListener(_:)),  
                                                      name: NSWorkspace.willSleepNotification, object: nil)  
    NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(sleepListener(_:)),  
                                                      name: NSWorkspace.didWakeNotification, object: nil)  
}  


@objc private func sleepListener(_ aNotification: Notification) {  
    print("listening to sleep")  
    if aNotification.name == NSWorkspace.willSleepNotification {  
        print("Going to sleep")  
    } else if aNotification.name == NSWorkspace.didWakeNotification {  
        print("Woke up")  
    } else {  
        print("Some other event other than the first two")  
    }  
}  
0

Source: https://habr.com/ru/post/1610020/


All Articles