With sysctl, you can get many informations about running processes. See in this code, you can find all running processes and also the started time of each process. This code is not private API so it should be accepted by Apple in case of posting it in Apple Store. Take a look in 'struct kinfo_proc' in sysctl.h, you will find useful infos. I don't know how to detect if a process is in foreground or background. I can find just the start_time, then to calculate running time. However, as you run this application in foreground, other processes are likely in background, isn't it?
#import <sys/sysctl.h>
- (NSArray *)runningProcesses
{
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
size_t miblen = 4;
size_t size;
int st = sysctl(mib, miblen, NULL, &size, NULL, 0);
struct kinfo_proc *process = NULL;
struct kinfo_proc *newprocess = NULL;
do {
size += size / 10;
newprocess = realloc(process, size);
if (!newprocess) {
if (process) {
free(process);
}
return nil;
}
process = newprocess;
st = sysctl(mib, miblen, process, &size, NULL, 0);
} while (st == -1 && errno == ENOMEM);
if (st == 0) {
if (size % sizeof(struct kinfo_proc) == 0) {
int nprocess = size / sizeof(struct kinfo_proc);
if (nprocess) {
NSMutableArray * array = [[NSMutableArray alloc] init];
for (int i = nprocess - 1; i >= 0; i--) {
NSString *processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];
NSString *processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];
struct timeval t = process[i].kp_proc.p_un.__p_starttime;
long ms = t.tv_sec;
NSDate *startDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:ms];
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, startDate, nil]
forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName",@"StartDate", nil]];
[array addObject:dict];
}
free(process);
return array;
}
}
}
return nil;
}