Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reliable way to get a machine wake + user logged in notification in Cocoa?

I've tried registering for NSWorkspaceDidWakeNotification with [NSNotificationCenter defaultCenter], but it never fires when my MacBook Pro wakes from sleep. Other notifications that I register for fire, so it's not an issue with how I'm registering for it.

Basically I need to get notified when a user's machine has woken from sleep, and the user has logged back in, or the machine has woken and the user is back in their account, in the case that the login screen wasn't displayed to them. If I have to handle that with multiple notifications, that's fine as well.

Any thoughts?

Thanks!
Cody

like image 824
codykrieger Avatar asked Feb 22 '12 20:02

codykrieger


Video Answer


1 Answers

The notification NSWorkspaceDidWakeNotification does not come from the default notification center. This works:

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                     selector:@selector(wakeFromSleep:)
                         name:NSWorkspaceDidWakeNotification
                       object:nil];

There exists the Apple Q&A 1340 regarding this question.

This covers only the waking aspect of your question. You don't get a notification when you log in originally, because you app is not running then. But you probably mean the password prompt for unlocking after sleep, and there you are fine.

like image 113
febeling Avatar answered Sep 28 '22 04:09

febeling