macOS系統(tǒng)的狀態(tài)切換及其響應(yīng)方法

在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



'系統(tǒng)偏好設(shè)置'里面的'桌面與屏幕保護(hù)程序'和'節(jié)能'

  • C.啟動'屏幕保護(hù)程序':需要選擇相應(yīng)的閑置時(shí)長
啟動'屏幕保護(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è)置相應(yīng)的放置時(shí)長可以進(jìn)行休眠
          當(dāng)前'節(jié)能'中設(shè)置的時(shí)長為‘1分鐘’

【一】、在系統(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í)長
當(dāng)前'節(jié)能'中設(shè)置的時(shí)長為‘1分鐘’

[$-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)及其通知!









goyohol's essay

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 升級win10后總是自動關(guān)屏休眠,各種電源管理設(shè)置不起作用(包括魯大師),最后找到關(guān)鍵原因,在屏保程序,具體如下:...
    PhageNanoenzyme閱讀 1,129評論 0 0
  • macOS 有多種方式實(shí)現(xiàn)鎖屏,常用的有以下兩種: 快捷鍵 觸發(fā)角 快捷鍵 Command + Ctrl + Q,...
    floodliu閱讀 5,561評論 0 2
  • 線程狀態(tài) 線程從創(chuàng)建到最終的消亡,要經(jīng)歷若干個(gè)狀態(tài)。一般來說,線程包括以下這幾個(gè)狀態(tài):創(chuàng)建(new)、就緒(run...
    郭藝賓閱讀 975評論 0 1
  • 表情是什么,我認(rèn)為表情就是表現(xiàn)出來的情緒。表情可以傳達(dá)很多信息。高興了當(dāng)然就笑了,難過就哭了。兩者是相互影響密不可...
    Persistenc_6aea閱讀 129,539評論 2 7
  • 16宿命:用概率思維提高你的勝算 以前的我是風(fēng)險(xiǎn)厭惡者,不喜歡去冒險(xiǎn),但是人生放棄了冒險(xiǎn),也就放棄了無數(shù)的可能。 ...
    yichen大刀閱讀 7,693評論 0 4

友情鏈接更多精彩內(nèi)容