在Swift OS X中无法收到NSWorkspaceDidWakeNotification [英] Cannot receive NSWorkspaceDidWakeNotification in Swift OS X

查看:208
本文介绍了在Swift OS X中无法收到NSWorkspaceDidWakeNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个macOS应用程序,在该应用程序中,当计算机唤醒时它们需要入睡并唤醒,但我无法使监听器正常工作.我觉得我已经尝试了一切.在AppDelegate.swift中,在函数applicationDidFinishLaunching中,我得到了:

I'm making a macOS 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)

并且在AppDelegate.swift内部,但是在函数applicationDidFinishLaunching外部,我有:

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");
}

我已经尝试了许多不同方法的组合来解决该问题.我试过听NSNotificationCenter.defaultCenter(),试过将选择器更改为sleepListener:wakeUpListener:,试过从两个函数中删除参数,但到目前为止没有任何效果.真正有趣的是,我还有另外两个侦听器NSWorkspaceScreensDidSleepNotificationNSWorkspaceScreensDidWakeNotification可以通过使用

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)

NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "screenWakeUpListener", name: NSWorkspaceScreensDidWakeNotification, object: nil)

引用功能

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.

提前谢谢!

推荐答案

我看到这篇文章来自很久以前.

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,甚至更好

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.

希望有帮助

这篇关于在Swift OS X中无法收到NSWorkspaceDidWakeNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆