您的当前位置:首页正文

Mac 中对系统Sleep 与 Wake 的检测

来源:华拓网

途径:notificationCenter

通过 NSWorkspace的notificationCenter中对相应通知,可知系统现在状态,通过通过相应委托进行处理

    func didReceivedWakeNote(note: NSNotification){
      //code
    }
    
    func didReceivedSleepNote(note: NSNotification){
      //code
    }
    
    func startBluetoothAfterWake(){
        NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "didReceivedWakeNote:", name: NSWorkspaceDidWakeNotification, object: nil)
    }
    
    func stopBluetoothBeforeSleep(){
        NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "didReceivedSleepNote:", name: NSWorkspaceWillSleepNotification, object: nil)
        
    }