Getting NSRunningApplication object from pid

837 views Asked by At

I'm a very beginner in Objective-c so i would appreciate your help... I need to check if some PID is a PID of active application. I want to do it by converting pid to NSRunningApplication and then use interface isActive. My question is how can I convert PID to NSRunningApplication? Or maybe there is other way to check if PID is active?

P.S I saw an implementation where you get the list of all applications and for each app you check if it is active. Then retrieve the pid from the active one. It seems not very efficient.

1

There are 1 answers

2
Sanich On
bool isApplicationActive(CFNumberRef applicationPID)
{
    NSRunningApplication* app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid_t(applicationPID)];
    if(app != nil)
    {
        return [app isActive];
    }
    return false;
}