当Mac从睡眠状态恢复时会发生什么事?

Lau*_*llo 10 macos events xcode cocoa sleep

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

谢谢 !

Lau*_*llo 10

刚发现它:

- (void) receiveWakeNote: (NSNotification*) note
{
    NSLog(@"receiveSleepNote: %@", [note name]);
}

- (void) fileNotifications
{
    //These notifications are filed on NSWorkspace's notification center, not the default 
    // notification center. You will not receive sleep/wake notifications if you file 
    //with the default notification center.
     [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self 
                                                           selector: @selector(receiveWakeNote:) 
                                                               name: NSWorkspaceDidWakeNotification object: NULL];
}
Run Code Online (Sandbox Code Playgroud)


And*_*kin 9

对于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)
}
Run Code Online (Sandbox Code Playgroud)

对于快速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)
}
Run Code Online (Sandbox Code Playgroud)