我想收集在作業系統中運行的應用程式的統計資訊。
所以我需要獲取應用程式啟動和退出的任何事件
有沒有存在一些?
谷歌什么也沒說=(
uj5u.com熱心網友回復:
您可以通過 收聽事件(例如NSWorkspaceDidLaunchApplicationNotification)NotificationCenter。請參閱以下檔案:
https://developer.apple.com/documentation/appkit/nsworkspacedidlaunchapplicationnotification
特別是,請查看 See also -> Responding to Environment Notifications 部分,其中列出了與此任務相關的許多通知。
uj5u.com熱心網友回復:
此代碼當前不起作用(2022.08)-由于錯誤https://github.com/apple/swift/pull/20757,change.oldValue和 change.newValue 目前為空
但必須在未來作業
class StatsCollector {
var observer: NSKeyValueObservation
init() {
self.observer = NSWorkspace.shared
.observe(\.runningApplications, options: [.initial]) { (model, change) in
//some code
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/511306.html
標籤:迅速目标-c苹果系统
