在快速OSX中无法接收NSWorkspaceDidWakeNotification

Sam*_*bel 5 macos notifications nsnotificationcenter nsworkspace swift

我正在制作一个mac应用程序,在该应用程序中,当计算机唤醒时,有必要做一些事情,但我无法使监听器正常工作。我觉得我已经尝试了一切。在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)
Run Code Online (Sandbox Code Playgroud)

在AppDelegate.swift内部,但在函数applicationDidFinishLaunching之外,我具有:

func sleepListener(aNotification : NSNotification) {
    print("Sleep Listening");
}

func wakeUpListener(aNotification : NSNotification) {
    print("Wake Up Listening");
}
Run Code Online (Sandbox Code Playgroud)

我尝试了许多不同方法的组合来解决此问题。我尝试侦听NSNotificationCenter.defaultCenter(),尝试将选择器更改为“ sleepListener:”和“ wakeUpListener:”,尝试从这两个函数中删除参数,但到目前为止没有任何效果。真正有趣的是,我还有另外两个侦听器可以完美地工作,它们分别是“ NSWorkspaceScreensDidSleepNotification”和“ NSWorkspaceScreensDidWakeNotification”。

    NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "screenSleepListener", name: NSWorkspaceScreensDidSleepNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)

    NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "screenWakeUpListener", name: NSWorkspaceScreensDidWakeNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)

引用功能

func screenSleepListener() {
    print("Screen Sleep Listening");
}

func screenWakeUpListener() {
    print("Screen Wake Up Listening");
}
Run Code Online (Sandbox Code Playgroud)

那么,这是我做错了吗?我应该提交错误报告吗?如果其他人可以在文件中运行此代码,则让他们的显示器和计算机进入睡眠状态,看看他们是否遇到相同的错误,那将非常有帮助。如果有人知道我做错了什么,那会更好。

先感谢您!

Cri*_*pto 5

我发现这个帖子是很久以前的了。

从您的帖子中,我得到的印象是您以错误的顺序进行了两次排列。

    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");
}
Run Code Online (Sandbox Code Playgroud)

或者

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");
}
Run Code Online (Sandbox Code Playgroud)

或者甚至更好

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")
            }
        }
Run Code Online (Sandbox Code Playgroud)

在哪里添加这些观察者也很重要。对我来说,他们都在应用程序委托中并且都工作。

希望有帮助