获取系统正在运行的应用-iOS

@interface UIDevice (ProcessesAdditions)
 - (NSArray *)runningProcesses;
 @end
 // .m
 #import <sys/sysctl.h>
 @implementation UIDevice (ProcessesAdditions)
 - (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];
                 
                                         NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil] 
                                                                                                                                                 forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil]];
                                         [processID release];
                                         [processName release];
                                         [array addObject:dict];
                                         [dict release];
                                 }
         
                                 free(process);
                                 return [array autorelease];
                         }
                 }
         }
         
         return nil;
 }
 @end
 // Example usage.
 NSArray * processes = [[UIDevice currentDevice] runningProcesses];
 for (NSDictionary * dict in processes){
         NSLog(@"%@ - %@", [dict objectForKey:@"ProcessID"], [dict objectForKey:@"ProcessName"]);
 }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值