Macos 无法在Swift OS X中接收NSWorkspaceDidWakeNotification
我正在制作一个macOS应用程序,在这个应用程序中,当计算机醒来、睡着、醒来时,有必要做一些事情,但我无法让侦听器工作。我觉得我什么都试过了。在Macos 无法在Swift OS X中接收NSWorkspaceDidWakeNotification,macos,swift,notifications,nsnotificationcenter,nsworkspace,Macos,Swift,Notifications,Nsnotificationcenter,Nsworkspace,我正在制作一个macOS应用程序,在这个应用程序中,当计算机醒来、睡着、醒来时,有必要做一些事情,但我无法让侦听器工作。我觉得我什么都试过了。在AppDelegate.swift函数applicationdFinishLaunching的内部,我得到了: NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "sleepListener", name: NSWorkspaceW
AppDelegate.swift
函数applicationdFinishLaunching
的内部,我得到了:
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "sleepListener", name: NSWorkspaceWillSleepNotification, object: nil)
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "wakeUpListener", name: NSWorkspaceDidWakeNotification, object: nil)
在AppDelegate.swift
内部,但在功能applicationdFinishLaunching
外部,我有:
func sleepListener(aNotification : NSNotification) {
print("Sleep Listening");
}
func wakeUpListener(aNotification : NSNotification) {
print("Wake Up Listening");
}
我尝试了多种不同的方法来解决这个问题。我尝试在NSNotificationCenter.defaultCenter()
上侦听,尝试将选择器更改为sleepListener:
和wakeUpListener:
,尝试从两个函数中删除参数,但迄今为止没有任何效果。真正有趣的是,我还有另外两个监听器可以很好地工作,nsworkspacesscreensidsleepnotification
和nsworkspacesscreensidwakenotification
,通过使用
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "screenSleepListener", name: NSWorkspaceScreensDidSleepNotification, object: nil)
及
引用函数
func screenSleepListener() {
print("Screen Sleep Listening");
}
func screenWakeUpListener() {
print("Screen 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() {
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")
}
}
在哪里添加这些观察者也很重要。对我来说,他们都在应用程序代理中,都在工作
希望对你有所帮助
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")
}
}
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")
}
}