iOS 9 之前,获取正在运行的进程列表的方法是使用sysctl这个方法
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);
process = NULL;
}
return nil;
}
process = newprocess;
st = sysctl(mib, miblen, process, &size, NULL, 0);
} while (st == -1 && errno == ENOMEM);
但iOS9之后,sysctl()方法已经禁止被沙盒中的App调用。
The first technique was to use the sysctl() function to retrieve the process table (a remnant of OS X), which includes the list of running Apps. In iOS 9, sysctl() was modified to no longer allow sandboxed Apps to retrieve information about other running processes.
How to get running process list in iOS 9?