在swift OSX

时间:2015-10-02 18:42:55

标签: macos swift notifications nsnotificationcenter nsworkspace

我正在制作一个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");
}

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

提前谢谢!

2 个答案:

答案 0 :(得分:3)

我看到这篇帖子是很久以前的。

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

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

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

您添加这些观察者的位置也很重要。对我来说,他们都是应用代表,他们都工作。

希望有所帮助

答案 1 :(得分:0)

快捷键5:

func applicationDidFinishLaunching(_ aNotification: Notification) {  
    NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(sleepListener(_:)),  
                                                      name: NSWorkspace.willSleepNotification, object: nil)  
    NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(sleepListener(_:)),  
                                                      name: NSWorkspace.didWakeNotification, object: nil)  
}  


@objc private func sleepListener(_ aNotification: Notification) {  
    print("listening to sleep")  
    if aNotification.name == NSWorkspace.willSleepNotification {  
        print("Going to sleep")  
    } else if aNotification.name == NSWorkspace.didWakeNotification {  
        print("Woke up")  
    } else {  
        print("Some other event other than the first two")  
    }  
}