From 12b0ade4bb945b0a51dca345253f45012a7ca30b Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 4 Sep 2023 11:15:09 +0800 Subject: [PATCH] Observe if display is off --- ProcessReporter/Utils/Reporter/Reporter.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ProcessReporter/Utils/Reporter/Reporter.swift b/ProcessReporter/Utils/Reporter/Reporter.swift index 1f48daf..1332bed 100644 --- a/ProcessReporter/Utils/Reporter/Reporter.swift +++ b/ProcessReporter/Utils/Reporter/Reporter.swift @@ -42,6 +42,22 @@ class Reporter { private var disposerList = [Disposable]() private func sleepHandler() { + let distributedNotificationCenter = DistributedNotificationCenter.default() + + distributedNotificationCenter.addObserver( + self, + selector: #selector(systemWillSleep), + name: NSNotification.Name("com.apple.screenIsLocked"), + object: nil + ) + + distributedNotificationCenter.addObserver( + self, + selector: #selector(systemDidWake), + name: NSNotification.Name("com.apple.screenIsUnlocked"), + object: nil + ) + NotificationCenter.default.addObserver(self, selector: #selector(systemWillSleep), name: NSWorkspace.willSleepNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(systemDidWake), name: NSWorkspace.didWakeNotification, object: nil) }