I’m new to Objective-C and programming in general but I’m beginning to grasp the syntax and have a mostly working application however I’m struggling with one part. I’d like to be able to use the application I’m writing to monitor the activity of another application, namely wether it’s open or not.
Ideally when a user clicks a button it will launch remote desktop client and then monitor when remote desktop client closes. I want to know when it closes so that I can either bring my application to the forefront or to restart the computer. Mostly my problems revolve around watching for when remote desktop client closes. Here’s what I was thinking of trying:
do {
NSArray* apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.microsoft.rdc"];
} while ([apps count] >= 1);
The problem with the approach you’ve posted is that that while loop will block the main thread, preventing your application from doing anything else. You could run that on a background thread to prevent that problem, but that’s probably not the best approach.
Instead, take a look at the NSWorkspace class’s notifications. One of them is NSWorkspaceDidTerminateApplicationNotification. You should be able to do something like this: