Skip to content

Instantly share code, notes, and snippets.

@Sam0230
Created July 31, 2021 17:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sam0230/9eac9250ba6a1570a4defb5d0966d415 to your computer and use it in GitHub Desktop.
Save Sam0230/9eac9250ba6a1570a4defb5d0966d415 to your computer and use it in GitHub Desktop.
Monitoring lock / unlock on macOS.
// import Foundation;
import UserNotifications;
let dnc = DistributedNotificationCenter.default();
let lockObserver = dnc.addObserver(forName: .init("com.apple.screenIsLocked"), object: nil, queue: .main) { _ in
print("lock");
exit(0);
};
let unlockObserver = dnc.addObserver(forName: .init("com.apple.screenIsUnlocked"), object: nil, queue: .main) { _ in
print("unlock");
exit(0);
};
RunLoop.main.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment