我正在制作一个 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:
and 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");
}
那么,这是我做错了吗?我应该提交错误报告吗?如果其他人可以在一个文件中运行此代码,让他们的显示器和他们的计算机进入睡眠状态,看看他们是否得到相同的错误,那将非常有帮助。如果有人知道我在世界上做错了什么,那就更好了。
先感谢您!