当Mac被锁定/醒来时会发生什么事件?

时间:2018-03-12 18:14:40

标签: swift macos

我试图在Mac被锁定或醒来时在我的应用中收到通知。

我使用IORequestIdle请求空闲,以编程方式锁定它。

在我的AppDelegate中,我添加了此功能来跟踪NSNotifcation:

NSWorkspace.shared().notificationCenter.addObserver(self, selector: #selector(sleepListener(aNotification:)), name: NSNotification.Name.NSWorkspaceWillSleep, object: nil)

NSWorkspace.shared().notificationCenter.addObserver(self, selector: #selector(sleepListener(aNotification:)), name: NSNotification.Name.NSWorkspaceDidWake, object: nil)

func sleepListener(aNotification : NSNotification) {

        print("Received notification")

        if aNotification.name == NSNotification.Name.NSWorkspaceWillSleep {
            print("Going to sleep")
        } else if aNotification.name == NSNotification.Name.NSWorkspaceDidWake {
            print("Woke up")
        } else {
            print("Some other event other than the first two")
        }
    }

但永远不会调用sleepListener函数。

0 个答案:

没有答案