代码如下:
.h
@interfaceUIDevice (ProcessesAdditions)
- (NSArray*)runningProcesses;
@end
.m
#import"UIDevice+ProcessesAdditions.h"
#import
@implementationUIDevice (ProcessesAdditions)
- (NSArray*)runningProcesses {
intmib[4] = {CTL_KERN,KERN_PROC,KERN_PROC_ALL, 0};
intmiblen = 4;
size_tsize;
intst =sysctl(mib, miblen,NULL, &size,NULL, 0);
structkinfo_proc* process =NULL;
structkinfo_proc* newprocess =NULL;
do{
size += size / 10;
newprocess =realloc(process, size);
if(!newprocess){
if(process){
free(process);
}
returnnil;
}
process = newprocess;
st =sysctl(mib, miblen, process, &size,NULL, 0);
}while(st == -1 &&errno==ENOMEM);
if(st == 0){
if(size %sizeof(structkinfo_proc) == 0){
longnprocess = size/sizeof(structkinfo_proc);
if(nprocess){
NSMutableArray* array = [[NSMutableArrayalloc]init];
for(longi = nprocess - 1; i >= 0; i--){
NSString* processID = [[NSStringalloc]initWithFormat:@"%d", process[i].kp_proc.p_pid];
NSString* processName = [[NSStringalloc]initWithFormat:@"%s", process[i].kp_proc.p_comm];
NSDictionary* dict = [[NSDictionaryalloc]initWithObjects:[NSArrayarrayWithObjects:processID, processName,nil]
forKeys:[NSArrayarrayWithObjects:@"ProcessID",@"ProcessName",nil]];
[processIDrelease];
[processNamerelease];
[arrayaddObject:dict];
[dictrelease];
}
free(process);
return[arrayautorelease];
}
}
}
returnnil;
}
@end
如何使用呢?
NSArray* processes = [[UIDevicecurrentDevice]runningProcesses];
for(NSDictionary* dictinprocesses){
NSLog(@"%@ - %@", [dictobjectForKey:@"ProcessID"], [dictobjectForKey:@"ProcessName"]);
}
以上代码在iOS8一下可以使用,在iOS9上加强了安全,不允许调用sysctl获取进程了。但是我觉得没有关系的,并发妨碍你做这件事 在非越狱的iPhone 6 (iOS 8.1.3) 上进行钓鱼攻击 (盗取App Store密码),可通过检查系统版本完成钓鱼。