加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

macos – 无法在swift OSX中接收NSWorkspaceDidWakeNotification

发布时间:2020-12-14 04:33:34 所属栏目:百科 来源:网络整理
导读:我正在制作一个mac应用程序,当计算机唤醒睡着并醒来时,有必要做一些事情,但我不能让听众工作.我觉得我已经尝试了一切.在AppDelegate. swift中,在函数applicationDidFinishLaunching中,我得到了: NSWorkspace.sharedWorkspace().notificationCenter.addObser
我正在制作一个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,object: nil)
    NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,object: nil)
func sleepListener() {
    print("Sleep Listening");
}

func wakeUpListener() {
    print("Wake Up Listening");
}

要么

NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,selector: "sleepListener:",object: nil)
        NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,selector: "wakeUpListener:",object: nil)

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

func wakeUpListener(aNotification : NSNotification) {
    print("Wake Up Listening");
}

或者甚至更好

NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,object: nil)

NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,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")
            }
        }

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

希望有所帮助

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读