在 Swift OS X 中无法接收 NSWorkspaceDidWakeNotification
Cannot receive NSWorkspaceDidWakeNotification in Swift OS X
我正在制作一个 macOS 应用程序,它需要在计算机唤醒、入睡和唤醒时做一些事情,但我无法让监听器工作。我觉得我已经尝试了一切。在 AppDelegate.swift
函数 applicationDidFinishLaunching
的内部,我有:
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "sleepListener", name: NSWorkspaceWillSleepNotification, object: nil)
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "wakeUpListener", name: NSWorkspaceDidWakeNotification, object: nil)
在 AppDelegate.swift
内部但在函数 applicationDidFinishLaunching
之外,我有:
func sleepListener(aNotification : NSNotification) {
print("Sleep Listening");
}
func wakeUpListener(aNotification : NSNotification) {
print("Wake Up Listening");
}
我尝试了很多不同的组合来解决这个问题。我试过听 NSNotificationCenter.defaultCenter()
,我试过将选择器更改为 sleepListener:
和 wakeUpListener:
,我试过从两个函数中删除参数,但到目前为止没有任何效果。真正有趣的是,我有另外两个监听器可以完美地工作,NSWorkspaceScreensDidSleepNotification
和 NSWorkspaceScreensDidWakeNotification
,通过调用它们
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "screenSleepListener", name: NSWorkspaceScreensDidSleepNotification, object: nil)
和
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "screenWakeUpListener", name: NSWorkspaceScreensDidWakeNotification, object: nil)
引用函数
func screenSleepListener() {
print("Screen Sleep Listening");
}
func screenWakeUpListener() {
print("Screen Wake Up Listening");
}
所以,这是我做错了什么吗?这是我应该提交错误报告的事情吗?如果其他人可以 运行 文件中的此代码,让他们的显示器和计算机进入睡眠状态,然后查看他们是否会遇到相同的错误,那将非常有帮助。如果有人知道我做错了什么,那就更好了。
提前致谢!
我看到这个post是很久以前的了。
从您的 post 来看,我的印象是您按错误的顺序进行了两次排列。
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")
}
}
在何处添加这些观察者也很重要。对我来说,他们都在 app delegate 中并且他们都工作了。
希望对您有所帮助
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")
}
}
我正在制作一个 macOS 应用程序,它需要在计算机唤醒、入睡和唤醒时做一些事情,但我无法让监听器工作。我觉得我已经尝试了一切。在 AppDelegate.swift
函数 applicationDidFinishLaunching
的内部,我有:
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "sleepListener", name: NSWorkspaceWillSleepNotification, object: nil)
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "wakeUpListener", name: NSWorkspaceDidWakeNotification, object: nil)
在 AppDelegate.swift
内部但在函数 applicationDidFinishLaunching
之外,我有:
func sleepListener(aNotification : NSNotification) {
print("Sleep Listening");
}
func wakeUpListener(aNotification : NSNotification) {
print("Wake Up Listening");
}
我尝试了很多不同的组合来解决这个问题。我试过听 NSNotificationCenter.defaultCenter()
,我试过将选择器更改为 sleepListener:
和 wakeUpListener:
,我试过从两个函数中删除参数,但到目前为止没有任何效果。真正有趣的是,我有另外两个监听器可以完美地工作,NSWorkspaceScreensDidSleepNotification
和 NSWorkspaceScreensDidWakeNotification
,通过调用它们
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "screenSleepListener", name: NSWorkspaceScreensDidSleepNotification, object: nil)
和
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "screenWakeUpListener", name: NSWorkspaceScreensDidWakeNotification, object: nil)
引用函数
func screenSleepListener() {
print("Screen Sleep Listening");
}
func screenWakeUpListener() {
print("Screen Wake Up Listening");
}
所以,这是我做错了什么吗?这是我应该提交错误报告的事情吗?如果其他人可以 运行 文件中的此代码,让他们的显示器和计算机进入睡眠状态,然后查看他们是否会遇到相同的错误,那将非常有帮助。如果有人知道我做错了什么,那就更好了。
提前致谢!
我看到这个post是很久以前的了。
从您的 post 来看,我的印象是您按错误的顺序进行了两次排列。
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")
}
}
在何处添加这些观察者也很重要。对我来说,他们都在 app delegate 中并且他们都工作了。
希望对您有所帮助
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")
}
}