Search code examples
swiftmacoscocoatime-trackinguser-inactivity

How to detect user inactivity in OS X writing in Swift Cocoa?


I have searched answers in stackoverflow and none of them matches my needs. I am creating time tracking app on Swift Cocoa macOS, like Hubstaff time tracking app. At the moment runs a timer and I want to detect user's inactivity after x period of time and to send a Notification that he has been Idle x period of time. I'm new to iOS and macOS development. Can I have an example of how to do it?

Here is my code:

import Cocoa

class ViewController: NSViewController {

@IBOutlet weak var label: NSTextField!
@IBOutlet weak var playImage: NSButton!

var timer : Timer!
var isTimerWorking : Bool = false
var startTime : Date!

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func playPause(_ sender: NSButton) {

    if isTimerWorking {
        endTimer()
        playImage.image = NSImage(named: NSImage.Name("play"))
        sender.state = .off
    } else {
        startTimer()
        playImage.image = NSImage(named: NSImage.Name("stop"))
        sender.state = .off
    }
}

func startTimer() {
    startTime = Date()
    timer = Timer.scheduledTimer(
        timeInterval: 1.0,
        target: self,
        selector: #selector(self.timerCounter),
        userInfo: nil,
        repeats: true
    )
    isTimerWorking = true
}

func endTimer() {
    if timer != nil {
        timer.invalidate()
        label.stringValue = "00:00:00"
    }
    isTimerWorking = false
}

@objc func timerCounter() {
    let currentTime = Date().timeIntervalSince(startTime)
    let hour   = Int(fmod(currentTime/3600, 60))
    let minute = Int(fmod(currentTime/60,   60))
    let second = Int(fmod(currentTime,      60))
    let hourValue   = String(format:"%02d", hour)
    let minuteValue = String(format:"%02d", minute)
    let secondValue = String(format:"%02d", second)
    label.stringValue = "\(hourValue):\(minuteValue):\(secondValue)"
}
}

Solution

  • In my own time tracking app I am using

    var lastEvent:CFTimeInterval = 0
    lastEvent = CGEventSource.secondsSinceLastEventType(CGEventSourceStateID.hidSystemState, eventType: CGEventType(rawValue: ~0)!)
    
    print(lastEvent)
    

    to get the user idle time.