iOS高级开发, 开机启动, 无限后台运行, 监听进程

非越狱情况下使用:

开机启动:App安装到IOS设备设备之后,无论App是否开启过,只要IOS设备重启,App就会随之启动;

无限后台运行:应用进入后台状态,可以无限后台运行,不被系统kill;

监听进程:可获IOS设备运行除系统外的App(包括正在运行和后台运行);


配置项目 plist文件

添加:

<key>UIBackgroundModes</key>

<array>

<string>voip</string>

</array>


功能类:ProccessHelper

[objc]  view plain copy
  1. #import <Foundation/Foundation.h>    
  2.     
  3. @interface ProccessHelper : NSObject    
  4.     
  5. + (NSArray *)runningProcesses;    
  6.     
  7. @end    
  8.   
  9. [cpp] view plaincopyprint?  
  10. #import "ProccessHelper.h"    
  11. //#include<objc/runtime.h>    
  12. #include <sys/sysctl.h>    
  13.     
  14. #include <stdbool.h>    
  15. #include <sys/types.h>    
  16. #include <unistd.h>    
  17. #include <sys/sysctl.h>    
  18.     
  19. @implementation ProccessHelper    
  20.     
  21. //You can determine if your app is being run under the debugger with the following code from    
  22. static bool AmIBeingDebugged(void)    
  23. // Returns true if the current process is being debugged (either    
  24. // running under the debugger or has a debugger attached post facto).    
  25. {    
  26.     int                 junk;    
  27.     int                 mib[4];    
  28.     struct kinfo_proc   info;    
  29.     size_t              size;    
  30.         
  31.     // Initialize the flags so that, if sysctl fails for some bizarre    
  32.     // reason, we get a predictable result.    
  33.         
  34.     info.kp_proc.p_flag = 0;    
  35.         
  36.     // Initialize mib, which tells sysctl the info we want, in this case    
  37.     // we're looking for information about a specific process ID.    
  38.         
  39.     mib[0] = CTL_KERN;    
  40.     mib[1] = KERN_PROC;    
  41.     mib[2] = KERN_PROC_PID;    
  42.     mib[3] = getpid();    
  43.         
  44.     // Call sysctl.    
  45.         
  46.     size = sizeof(info);    
  47.     junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL0);    
  48.     assert(junk == 0);    
  49.         
  50.     // We're being debugged if the P_TRACED flag is set.    
  51.         
  52.     return ( (info.kp_proc.p_flag & P_TRACED) != 0 );    
  53. }    
  54.     
  55. //返回所有正在运行的进程的 id,name,占用cpu,运行时间    
  56. //使用函数int   sysctl(int *, u_int, void *, size_t *, void *, size_t)    
  57. + (NSArray *)runningProcesses    
  58. {    
  59.     //指定名字参数,按照顺序第一个元素指定本请求定向到内核的哪个子系统,第二个及其后元素依次细化指定该系统的某个部分。    
  60.     //CTL_KERN,KERN_PROC,KERN_PROC_ALL 正在运行的所有进程    
  61.     int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL ,0};    
  62.         
  63.         
  64.     size_t miblen = 4;    
  65.     //值-结果参数:函数被调用时,size指向的值指定该缓冲区的大小;函数返回时,该值给出内核存放在该缓冲区中的数据量    
  66.     //如果这个缓冲不够大,函数就返回ENOMEM错误    
  67.     size_t size;    
  68.     //返回0,成功;返回-1,失败    
  69.     int st = sysctl(mib, miblen, NULL, &size, NULL0);    
  70.         
  71.     struct kinfo_proc * process = NULL;    
  72.     struct kinfo_proc * newprocess = NULL;    
  73.     do    
  74.     {    
  75.         size += size / 10;    
  76.         newprocess = realloc(process, size);    
  77.         if (!newprocess)    
  78.         {    
  79.             if (process)    
  80.             {    
  81.                 free(process);    
  82.                 process = NULL;    
  83.             }    
  84.             return nil;    
  85.         }    
  86.             
  87.         process = newprocess;    
  88.         st = sysctl(mib, miblen, process, &size, NULL0);    
  89.     } while (st == -1 && errno == ENOMEM);    
  90.         
  91.     if (st == 0)    
  92.     {    
  93.         if (size % sizeof(struct kinfo_proc) == 0)    
  94.         {    
  95.             int nprocess = size / sizeof(struct kinfo_proc);    
  96.             if (nprocess)    
  97.             {    
  98.                 NSMutableArray * array = [[NSMutableArray alloc] init];    
  99.                 for (int i = nprocess - 1; i >= 0; i--)    
  100.                 {    
  101.                     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];    
  102.                     NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];    
  103.                     NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];    
  104.                     NSString * proc_CPU = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_estcpu];    
  105.                     double t = [[NSDate date] timeIntervalSince1970] - process[i].kp_proc.p_un.__p_starttime.tv_sec;    
  106.                     NSString * proc_useTiem = [[NSString alloc] initWithFormat:@"%f",t];    
  107.                     NSString *startTime = [[NSString alloc] initWithFormat:@"%ld", process[i].kp_proc.p_un.__p_starttime.tv_sec];    
  108.                     NSString * status = [[NSString alloc] initWithFormat:@"%d",process[i].kp_proc.p_flag];    
  109.                         
  110.                     NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];    
  111.                     [dic setValue:processID forKey:@"ProcessID"];    
  112.                     [dic setValue:processName forKey:@"ProcessName"];    
  113.                     [dic setValue:proc_CPU forKey:@"ProcessCPU"];    
  114.                     [dic setValue:proc_useTiem forKey:@"ProcessUseTime"];    
  115.                     [dic setValue:proc_useTiem forKey:@"ProcessUseTime"];    
  116.                     [dic setValue:startTime forKey:@"startTime"];    
  117.                         
  118.                     // 18432 is the currently running application    
  119.                     // 16384 is background    
  120.                     [dic setValue:status forKey:@"status"];    
  121.                         
  122.                     [processID release];    
  123.                     [processName release];    
  124.                     [proc_CPU release];    
  125.                     [proc_useTiem release];    
  126.                     [array addObject:dic];    
  127.                     [startTime release];    
  128.                     [status release];    
  129.                     [dic release];    
  130.                         
  131.                     [pool release];    
  132.                 }    
  133.                     
  134.                 free(process);    
  135.                 process = NULL;    
  136.                 //NSLog(@"array = %@",array);    
  137.                     
  138.                 return array;    
  139.             }    
  140.         }    
  141.     }    
  142.         
  143.     return nil;    
  144. }    
  145.     
  146. @end    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值