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");
}
, , ? -, ? - , , , , . - , , .
!