问题
I'm making a mac application where it's necessary to do something when the computer wakes falls asleep and wakes up, but I can't get the listener to work. I feel like I've tried everything. In AppDelegate.swift, inside of the function applicationDidFinishLaunching, I've got:
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 of the function applicationDidFinishLaunching, I have:
func sleepListener(aNotification : NSNotification) {
print("Sleep Listening");
}
func wakeUpListener(aNotification : NSNotification) {
print("Wake Up Listening");
}
I've tried a combination of many different things to fix the problem. I tried listening on NSNotificationCenter.defaultCenter(), I've tried changing the selector to "sleepListener:" and "wakeUpListener:", I've tried removing the arguments from both functions, and nothing has worked so far. And the really interesting thing is that I have got two other listeners to work perfectly, "NSWorkspaceScreensDidSleepNotification" and "NSWorkspaceScreensDidWakeNotification", by calling 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)
referencing the functions
func screenSleepListener() {
print("Screen Sleep Listening");
}
func screenWakeUpListener() {
print("Screen Wake Up Listening");
}
So, is this something I'm doing wrong? Is it something I should file a bug report about? If somebody else could run this code in a file, let their display and their computer go to sleep, and see if they get the same errors, that would be extremely helpful. And if somebody knows what in the world I'm doing wrong, that would be even better.
Thank you in advance!
回答1:
I see that this post was from a long time ago.
From your post, I get the impression that you did two permutations in the wrong order.
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");
}
OR
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");
}
OR and even better yet
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")
}
}
It also matter where you're adding these observers. For me, they were both in the app delegate and they both worked.
Hope that helps
回答2:
Swift 5:
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")
}
}
来源:https://stackoverflow.com/questions/32913789/cannot-receive-nsworkspacedidwakenotification-in-swift-osx