我试图在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
函数。