Detect Mac OS X Screen Lock & Unlock With Objective-C

I really couldn’t find very much on Google about how to detect when OS X has it’s screen locked. Screen lock is not the same as the log in window; screen lock is basically just a system overlay that keeps everything running but locked and secure, while on the other hand the log in screen completely logs the user out and kills the session.

Screen lock is a function that can be activated in the Security panel of System Preferences, but also the Keychain menu bar application can lock the screen for you, or you can press ctrl+shift+eject/power.

I finally stumbled upon this answer on StackOverflow, and in case that link ever dies I thought I would add it here too.

In your .m file you’ll need to set up a  NSDistributedNotificationCenter which listens for screen locked notifications:

And then of course implement the two selectors: