I want to collect statistics for applications runned in OS.
So I need to get any events of app start and exit
Is there exist some?
Google say nothing =(
CodePudding user response:
You can listen to events (such as NSWorkspaceDidLaunchApplicationNotification
) via NotificationCenter
. See the following documentation:
https://developer.apple.com/documentation/appkit/nsworkspacedidlaunchapplicationnotification
In particular, look at the See also -> Responding to Environment Notifications section, which lists many relevant notifications to this task.
CodePudding user response:
class StatsCollector {
var observer: NSKeyValueObservation
init() {
self.observer = NSWorkspace.shared
.observe(\.runningApplications, options: [.initial]) { (model, change) in
//some code
}
}
}