Handling NSWorkspace notifications for application launch and termination in macOS 13.6
I've searched everywhere and can't find a clear answer... I'm having trouble with I'm collaborating on a project where I've looked through the documentation and I'm still confused about I'm trying to monitor when other applications are launched or terminated in my macOS app (built using AppKit on macOS 13.6)..... Although I registered for notifications using `NSWorkspace.shared.notificationCenter`, I find that the notifications for application termination are not being fired consistently, especially if the app is force-quit. Hereβs what I have so far: ```swift import Cocoa class AppObserver: NSObject { override init() { super.init() NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(applicationLaunched), name: NSApplication.didLaunchNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(applicationTerminated), name: NSApplication.willTerminateNotification, object: nil) } @objc func applicationLaunched(notification: Notification) { if let userInfo = notification.userInfo, let app = userInfo[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication { print("Application launched: \(app.localizedName ?? \"Unknown\")") } } @objc func applicationTerminated(notification: Notification) { if let userInfo = notification.userInfo, let app = userInfo[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication { print("Application terminated: \(app.localizedName ?? \"Unknown\")") } } } ``` I have confirmed that the `applicationLaunched` method works well and fires as expected when applications are launched. However, the `applicationTerminated` method only seems to trigger when applications close normally. If I force-quit an app, those notifications donβt appear at all, and I'm trying to figure out why. I've also tried switching to `NSWorkspace.didTerminateApplicationNotification`, but that only seems to trigger for applications that finish closing correctly. I suspect this might be a limitation in how macOS handles these notifications for forcibly terminated applications. Is there a different approach or workaround to reliably catch all application termination events, including force-quits? Any insights on best practices for handling this scenario would be greatly appreciated! For context: I'm using Swift on Ubuntu. Has anyone else encountered this? Cheers for any assistance! This is my first time working with Swift latest. Any feedback is welcome!