gpt4 book ai didi

xcode - Mac 从 sleep 状态恢复时会触发什么事件?

转载 作者:行者123 更新时间:2023-12-03 16:04:30 27 4
gpt4 key购买 nike

我正在 Mac 上的 Xcode 中开发一个应用程序,想知道当 Mac 从 sleep 状态恢复时触发的事件。AwakeFromNib 似乎不起作用。

最佳答案

对于 swift 3:

func onWakeNote(note: NSNotification) {
print("Received wake note: \(note.name)")
}

func onSleepNote(note: NSNotification) {
print("Received sleep note: \(note.name)")
}

func fileNotifications() {
NSWorkspace.shared().notificationCenter.addObserver(
self, selector: #selector(onWakeNote(note:)),
name: Notification.Name.NSWorkspaceDidWake, object: nil)

NSWorkspace.shared().notificationCenter.addObserver(
self, selector: #selector(onSleepNote(note:)),
name: Notification.Name.NSWorkspaceWillSleep, object: nil)
}

对于 swift 4:

@objc func onWakeNote(note: NSNotification) {
...
}

@objc func onSleepNote(note: NSNotification) {
...
}

func fileNotifications() {
NSWorkspace.shared.notificationCenter.addObserver(
self, selector: #selector(onWakeNote(note:)),
name: NSWorkspace.didWakeNotification, object: nil)

NSWorkspace.shared.notificationCenter.addObserver(
self, selector: #selector(onSleepNote(note:)),
name: NSWorkspace.willSleepNotification, object: nil)
}

关于xcode - Mac 从 sleep 状态恢复时会触发什么事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9247710/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com