当前位置 : 主页 > 手机开发 > 其它 >

macos – 无法在swift OSX中接收NSWorkspaceDidWakeNotification

来源:互联网 收集:自由互联 发布时间:2021-06-11
我正在制作一个mac应用程序,当计算机唤醒睡着并醒来时,有必要做一些事情,但我不能让听众工作.我觉得我已经尝试了一切.在AppDelegate. swift中,在函数applicationDidFinishLaunching中,我得到了:
我正在制作一个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)

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

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

先感谢您!

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

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

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

在你添加这些观察者的地方也很重要.对我来说,他们都是应用代表,他们都工作.

希望有所帮助

网友评论