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:


Kane Cheshire

Author: Kane Cheshire

I'm a software developer (and sort of designer) from Hertfordshire. I spend a lot of time learning, travelling and flying my drone. I made MacID. You can check out some of my favourite photographs from travelling at I'm also the creator and lover of Twicnic, an annual Twitter picnic event.

Leave a Reply

Your email address will not be published. Required fields are marked *