在macOS的程序開發(fā)中,經(jīng)常需要知道系統(tǒng)一些狀態(tài)的改變而去對程序 做出一定的處理!比如收到系統(tǒng)睡眠、喚醒、切換用戶、屏幕關(guān)閉、顯示屏保等系統(tǒng)狀態(tài)。
-
大部分的系統(tǒng)狀態(tài),通過“
NSWorkspace .shared .notificationCenter”來實(shí)現(xiàn):
NSWorkspace .shared .notificationCenter .addObserver(self, selector: #selector(methodWillSleepNotification), name: NSWorkspace.willSleepNotification, object: nil)
NSWorkspace .shared .notificationCenter .addObserver(self, selector: #selector(methodDidWakeNotification), name: NSWorkspace.didWakeNotification, object: nil)
NSWorkspace .shared .notificationCenter .addObserver(self, selector: #selector(methodScreensDidSleepNotification), name: NSWorkspace.screensDidSleepNotification, object: nil)
NSWorkspace .shared .notificationCenter .addObserver(self, selector: #selector(methodScreensDidWakeNotification), name: NSWorkspace.screensDidWakeNotification, object: nil)
NSWorkspace .shared .notificationCenter .addObserver(self, selector: #selector(methodWillPowerOffNotification), name: NSWorkspace.willPowerOffNotification, object: nil)
NSWorkspace .shared .notificationCenter .addObserver(self, selector: #selector(methodSessionDidResignActiveNotification), name: NSWorkspace.sessionDidResignActiveNotification, object: nil)
NSWorkspace .shared .notificationCenter .addObserver(self, selector: #selector(methodSessionDidBecomeActiveNotification), name: NSWorkspace.sessionDidBecomeActiveNotification, object: nil)
相應(yīng)的響應(yīng)方法:
//睡眠 //NSWorkspace.willSleepNotification
@objc func methodWillSleepNotification () {
print("methodWillSleepNotification")
}
//從睡眠中喚醒 //NSWorkspace.didWakeNotification
@objc func methodDidWakeNotification () {
print("methodDidWakeNotification")
}
//屏幕睡眠 //NSWorkspace.screensDidSleepNotification
@objc func methodScreensDidSleepNotification () {
print("methodScreensDidSleepNotification")
}
//屏幕喚醒 //NSWorkspace.screensDidWakeNotification
@objc func methodScreensDidWakeNotification () {
print("methodScreensDidWakeNotification")
}
//當(dāng)用戶注銷或關(guān)機(jī) //NSWorkspace.willPowerOffNotification
@objc func methodWillPowerOffNotification () {
print("methodWillPowerOffNotification")
}
//被切換到另一用戶 //NSWorkspace.sessionDidResignActiveNotification
@objc func methodSessionDidResignActiveNotification () {
print("methodSessionDidResignActiveNotification")
}
//被切換回到當(dāng)前用戶 //NSWorkspace.sessionDidBecomeActiveNotification
@objc func methodSessionDidBecomeActiveNotification () {
print("methodSessionDidBecomeActiveNotification")
}
- 對于某一些特殊系統(tǒng)狀態(tài)(如‘屏?!?‘鎖定屏幕’等),就需要NSDistributedNotificationCenter(跨進(jìn)程的通知)來實(shí)現(xiàn):
//對于某一些系統(tǒng)狀態(tài),就需要以下的DistributedNotificationCenter(跨進(jìn)程的通知)來實(shí)現(xiàn):
DistributedNotificationCenter .default() .addObserver(self, selector: #selector(method_screenSaverDidStart), name: NSNotification.Name(rawValue: "com.apple.screensaver.didstart"), object: nil)
DistributedNotificationCenter .default() .addObserver(self, selector: #selector(method_screenSaverWillStop), name: NSNotification.Name(rawValue: "com.apple.screensaver.willstop"), object: nil)
DistributedNotificationCenter .default() .addObserver(self, selector: #selector(method_screenSaverDidStop), name: NSNotification.Name(rawValue: "com.apple.screensaver.didstop"), object: nil)
DistributedNotificationCenter .default() .addObserver(self, selector: #selector(method_screenIsLocked), name: NSNotification.Name(rawValue: "com.apple.screenIsLocked"), object: nil)
DistributedNotificationCenter .default() .addObserver(self, selector: #selector(method_screenIsUnlocked), name: NSNotification.Name(rawValue: "com.apple.screenIsUnlocked"), object: nil)
相應(yīng)的響應(yīng)方法:
//屏保開始 //"com.apple.screensaver.didstart"
@objc func method_screenSaverDidStart() {
print("method_screenSaverDidStart")
}
//屏保將要結(jié)束 //"com.apple.screensaver.willstop"
@objc func method_screenSaverWillStop() {
print("method_screenSaverWillStop")
}
//屏保結(jié)束 //"com.apple.screensaver.didstop"
@objc func method_screenSaverDidStop() {
print("method_screenSaverDidStop")
}
//屏幕鎖住 //"com.apple.screenIsLocked"
@objc func method_screenIsLocked() {
print("method_screenIsLocked")
}
//屏幕解鎖 //"com.apple.screenIsUnlocked"
@objc func method_screenIsUnlocked() {
print("method_screenIsUnlocked")
}
- A.直接選擇屏幕頂部菜單欄?對應(yīng)的'睡眠'項(xiàng):

打印如下:
methodWillSleepNotification
methodScreensDidSleepNotification
method_screenIsLocked
method_screenIsUnlocked
methodDidWakeNotification
methodScreensDidWakeNotification
其中,點(diǎn)擊'睡眠'項(xiàng)后進(jìn)行睡眠的打?。?/p>
methodWillSleepNotification methodScreensDidSleepNotification method_screenIsLocked重新喚醒后的打?。?/p>
method_screenIsUnlocked methodDidWakeNotification methodScreensDidWakeNotification
- B.直接選擇頂部菜單欄?對應(yīng)的'鎖定屏幕'項(xiàng):

打印如下:
method_screenIsLocked
method_screenIsUnlocked
其中,點(diǎn)擊'鎖定屏幕'項(xiàng)后進(jìn)行鎖屏的打印:
method_screenIsLocked重新輸入該賬戶密碼,解鎖屏幕后的打印:
method_screenIsUnlocked

- C.啟動'屏幕保護(hù)程序':需要選擇相應(yīng)的閑置時(shí)長

- C-1.自動彈出了屏幕保護(hù),但是系統(tǒng)還未進(jìn)入休眠:
- 【一】、在系統(tǒng)偏好設(shè)置的'節(jié)能'中設(shè)置為'永不'—永遠(yuǎn)不進(jìn)行休眠
當(dāng)前'節(jié)能'中設(shè)置的時(shí)長為‘永不’
- 【一】、在系統(tǒng)偏好設(shè)置的'節(jié)能'中設(shè)置為'永不'—永遠(yuǎn)不進(jìn)行休眠
- 【二】、在系統(tǒng)偏好設(shè)置的'節(jié)能'中設(shè)置相應(yīng)的放置時(shí)長—可以進(jìn)行休眠
當(dāng)前'節(jié)能'中設(shè)置的時(shí)長為‘1分鐘’
- 【二】、在系統(tǒng)偏好設(shè)置的'節(jié)能'中設(shè)置相應(yīng)的放置時(shí)長—可以進(jìn)行休眠
【一】、在系統(tǒng)偏好設(shè)置的'節(jié)能'中設(shè)置為'永不'——不管是 (已經(jīng)被鎖屏)需要輸入用戶解鎖屏幕的密碼、(還沒有被鎖屏)還不需要輸入用戶解鎖屏幕的密碼!
【二】、在系統(tǒng)偏好設(shè)置的'節(jié)能'中設(shè)置為‘1分鐘’——只是出現(xiàn)了屏幕保護(hù)(系統(tǒng)還未休眠)就激活電腦來退出了屏幕保護(hù)!
上面的情況【一】、【二】,打印都如下:
method_screenSaverDidStart
method_screenIsLocked
method_screenSaverWillStop
method_screenSaverDidStop
method_screenIsUnlocked
其中,自動彈出了屏幕保護(hù)的打?。?/p>
method_screenSaverDidStart method_screenIsLocked解鎖屏幕后的打印:(不管是否需要重新輸入該賬戶密碼)
method_screenSaverWillStop method_screenSaverDidStop method_screenIsUnlocked
- C-2.自動彈出了屏幕保護(hù),并再放置到系統(tǒng)睡眠(系統(tǒng)偏好設(shè)置的'節(jié)能'中,設(shè)置相應(yīng)的放置時(shí)長)

[$-1].不需要重新輸入該賬戶密碼:
method_screenSaverDidStart
method_screenIsLocked
methodScreensDidSleepNotification
method_screenSaverWillStop
method_screenSaverDidStop
method_screenSaverWillStop
method_screenSaverDidStop
method_screenIsUnlocked
methodScreensDidWakeNotification
[$-2].需要重新輸入該賬戶密碼:
method_screenSaverDidStart
method_screenIsLocked
methodScreensDidSleepNotification
method_screenSaverWillStop
method_screenSaverDidStop
method_screenSaverWillStop
method_screenSaverDidStop
methodScreensDidWakeNotification
method_screenIsUnlocked
其中,自動彈出了屏幕保護(hù),并再放置到系統(tǒng)睡眠
method_screenSaverDidStart method_screenIsLocked methodScreensDidSleepNotification解鎖屏幕后的打印:
- [$-1].不需要重新輸入該賬戶密碼
method_screenSaverWillStop method_screenSaverDidStop method_screenSaverWillStop method_screenSaverDidStop method_screenIsUnlocked methodScreensDidWakeNotification
- [$-2].需要重新輸入該賬戶密碼
method_screenSaverWillStop method_screenSaverDidStop method_screenSaverWillStop method_screenSaverDidStop methodScreensDidWakeNotification method_screenIsUnlocked
更多系統(tǒng)狀態(tài)相應(yīng)響應(yīng)方法的順序——根據(jù)你自己開發(fā)App的需求,就自己試出其調(diào)用順序再搭配使用吧~
OC代碼:
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(sleepMethod:) name:NSWorkspaceWillSleepNotification object:nil]; //其中能捕獲到的狀態(tài)有: //NSWorkspaceWillSleepNotification //睡眠 //NSWorkspaceDidWakeNotification //從睡眠中喚醒 //NSWorkspaceWillPowerOffNotification //當(dāng)用戶注銷或關(guān)機(jī) //NSWorkspaceSessionDidResignActiveNotification //被切換到另一用戶 //NSWorkspaceSessionDidBecomeActiveNotification //被切換回到當(dāng)前用戶 //NSWorkspaceScreensDidSleepNotification //屏幕睡眠 //NSWorkspaceScreensDidWakeNotification //屏幕喚醒需要去實(shí)現(xiàn)其響應(yīng)方法(
sleepMethod:)~
對于某一些系統(tǒng)狀態(tài)(如‘屏?!?‘鎖定屏幕’等),就需要NSDistributedNotificationCenter(跨進(jìn)程的通知)來實(shí)現(xiàn):[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(screensaverStart:) name:@"com.apple.screensaver.didstart" object:nil]; //其中的通知名稱可以是: //com.apple.screensaver.didstart //屏保開始 //com.apple.screensaver.willstop //屏保將要結(jié)束 //com.apple.screensaver.didstop //屏保結(jié)束 //com.apple.screenIsLocked //屏幕鎖住 //com.apple.screenIsUnlocked //屏幕解鎖同樣需要去實(shí)現(xiàn)其響應(yīng)方法(
screensaverStart:)~
以上便是關(guān)于macOS系統(tǒng)狀態(tài)的討論~
關(guān)于NSApp對應(yīng)的App狀態(tài)討論,請參考《NSApp — App狀態(tài)及其通知》!

