非越狱情况下实现:

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

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

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

配置项目 plist文件

添加:

<key>UIBackgroundModes</key>

<array>

<string>voip</string>

</array>

功能类:ProccessHelper

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

实现代码:

  1. systemprocessArray = [[NSMutableArray arrayWithObjects:
  2. @"kernel_task",
  3. @"launchd",
  4. @"UserEventAgent",
  5. @"wifid",
  6. @"syslogd",
  7. @"powerd",
  8. @"lockdownd",
  9. @"mediaserverd",
  10. @"mediaremoted",
  11. @"mDNSResponder",
  12. @"locationd",
  13. @"imagent",
  14. @"iapd",
  15. @"fseventsd",
  16. @"fairplayd.N81",
  17. @"configd",
  18. @"apsd",
  19. @"aggregated",
  20. @"SpringBoard",
  21. @"CommCenterClassi",
  22. @"BTServer",
  23. @"notifyd",
  24. @"MobilePhone",
  25. @"ptpd",
  26. @"afcd",
  27. @"notification_pro",
  28. @"notification_pro",
  29. @"syslog_relay",
  30. @"notification_pro",
  31. @"springboardservi",
  32. @"atc",
  33. @"sandboxd",
  34. @"networkd",
  35. @"lsd",
  36. @"securityd",
  37. @"lockbot",
  38. @"installd",
  39. @"debugserver",
  40. @"amfid",
  41. @"AppleIDAuthAgent",
  42. @"BootLaunch",
  43. @"MobileMail",
  44. @"BlueTool",
  45. nil] retain];
  1. - (void)applicationDidEnterBackground:(UIApplication *)application
  2. {
  3. while (1) {
  4. sleep(5);
  5. [self postMsg];
  6. }
    1. [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{
    2. NSLog(@"KeepAlive");
    3. }];
    4. }
    5. - (void)applicationWillResignActive:(UIApplication *)application
    6. {
    7. }
    8. - (void)applicationWillEnterForeground:(UIApplication *)application
    9. {
    10. }
    11. - (void)applicationDidBecomeActive:(UIApplication *)application
    12. {
    13. }
    14. - (void)applicationWillTerminate:(UIApplication *)application
    15. {
    16. }
    17. #pragma mark -
    18. #pragma mark - User Method
    19. - (void) postMsg
    20. {
    21. //上传到服务器
    22. NSURL *url = [self getURL];
    23. NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    24. NSError *error = nil;
    25. NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    26. if (error) {
    27. NSLog(@"error:%@", [error localizedDescription]);
    28. }
    29. NSString *str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
    30. NSLog(@"%@",str);
    31. }
    32. - (NSURL *) getURL
    33. {
    34. UIDevice *device = [UIDevice currentDevice];
    35. NSString* uuid = @"TESTUUID";
    36. NSString* manufacturer = @"apple";
    37. NSString* model = [device model];
    38. NSString* mobile = [device systemVersion];
    39. NSString *msg = [NSString stringWithFormat:@"Msg:%@  Time:%@", [self processMsg], [self getTime]];
    40. CFShow(msg);
    41. /  省略部分代码  /
    42. NSString *urlStr = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    43. NSURL *url = [NSURL URLWithString:urlStr];
    44. return url;
    45. }
    46. - (BOOL) checkSystemProccess:(NSString *) proName
    47. {
    48. if ([systemprocessArray containsObject:proName]) {
    49. return YES;
    50. }
    51. return NO;
    52. }
    53. - (BOOL) checkFirst:(NSString *) string
    54. {
    55. NSString *str = [string substringToIndex:1];
    56. NSRange r = [@"ABCDEFGHIJKLMNOPQRSTUVWXWZ" rangeOfString:str];
    57. if (r.length > 0) {
    58. return YES;
    59. }
    60. return NO;
    61. }
    62. - (NSString *) processMsg
    63. {
    64. NSArray *proMsg = [ProccessHelper runningProcesses];
    65. if (proMsg == nil) {
    66. return nil;
    67. }
    68. NSMutableArray *proState = [NSMutableArray array];
    69. for (NSDictionary *dic in proMsg) {
    70. NSString *proName = [dic objectForKey:@"ProcessName"];
    71. if (![self checkSystemProccess:proName] && [self checkFirst:proName]) {
    72. NSString *proID = [dic objectForKey:@"ProcessID"];
    73. NSString *proStartTime = [dic objectForKey:@"startTime"];
    74. if ([[dic objectForKey:@"status"] isEqualToString:@"18432"]) {
    75. NSString *msg = [NSString stringWithFormat:@"ProcessName:%@ - ProcessID:%@ - StartTime:%@ Running:YES", proName, proID, proStartTime];
    76. [proState addObject:msg];
    77. } else {
    78. NSString *msg = [NSString stringWithFormat:@"ProcessName:%@ - ProcessID:%@ - StartTime:%@ Running:NO", proName, proID, proStartTime];
    79. [proState addObject:msg];
    80. }
    81. }
    82. }
    83. NSString *msg = [proState componentsJoinedByString:@"______"];
    84. return msg;
    85. }
    86. // 获取时间
    87. - (NSString *) getTime
    88. {
    89. NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];
    90. formatter.dateStyle = NSDateFormatterMediumStyle;
    91. formatter.timeStyle = NSDateFormatterMediumStyle;
    92. formatter.locale = [NSLocale currentLocale];
    93. NSDate *date = [NSDate date];
    94. [formatter setTimeStyle:NSDateFormatterMediumStyle];
    95. NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    96. NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
    97. NSInteger unitFlags = NSYearCalendarUnit |
    98. NSMonthCalendarUnit |
    99. NSDayCalendarUnit |
    100. NSWeekdayCalendarUnit |
    101. NSHourCalendarUnit |
    102. NSMinuteCalendarUnit |
    103. NSSecondCalendarUnit;
    104. comps = [calendar components:unitFlags fromDate:date];
    105. int year = [comps year];
    106. int month = [comps month];
    107. int day = [comps day];
    108. int hour = [comps hour];
    109. int min = [comps minute];
    110. int sec = [comps second];
    111. NSString *time = [NSString stringWithFormat:@"%d-%d-%d %d:%d:%d", year, month, day, hour, min, sec];
    112. return time;
    113. }
    114. @end

IOS开发~开机启动&无限后台运行&监听进程的更多相关文章

  1. IOS高级开发~开机启动&无限后台运行&监听进程

    一般来说, IOS很少给App后台运行的权限. 仅有的方式就是 VoIP. IOS少有的为VoIP应用提供了后台socket连接,定期唤醒并且随开机启动的权限.而这些就是IOS上实现VoIP App的 ...

  2. 安卓开机启动service后台运行

    安卓开机启动service后台运行 Android开机启动时会发送一个广播android.intent.action.BOOT_COMPLETED,捕捉到这个广播,然后可以进行相应的操作,比如:通过捕 ...

  3. iOS开发UI篇—无限轮播(循环展示)

    iOS开发UI篇—无限轮播(循环展示) 一.简单说明 之前的程序还存在一个问题,那就是不能循环展示,因为plist文件中只有五个数组,因此第一个和最后一个之后就没有了,下面介绍处理这种循环展示问题的小 ...

  4. iOS开发UI篇—无限轮播(功能完善)

    iOS开发UI篇—无限轮播(功能完善) 一.自动滚动 添加并设置一个定时器,每个2.0秒,就跳转到下一条. 获取当前正在展示的位置. [self addNSTimer]; } -(void)addNS ...

  5. iOS开发UI篇—无限轮播(循环利用)

    iOS开发UI篇—无限轮播(循环利用) 一.无限轮播  1.简单说明 在开发中常需要对广告或者是一些图片进行自动的轮播,也就是所谓的无限滚动. 在开发的时候,我们通常的做法是使用一个UIScrollV ...

  6. iOS开发UI篇—无限轮播(新闻数据展示)

    iOS开发UI篇—无限轮播(新闻数据展示) 一.实现效果        二.实现步骤 1.前期准备 (1)导入数据转模型的第三方框架MJExtension (2)向项目中添加保存有“新闻”数据的pli ...

  7. 【iOS系列】-程序开启后台运行

    [iOS系列]-程序开启后台运行 iOS程序是伪后台的运行,可是有时候我们需要让其在后台也要进行一些操作,我们可以让其伪装成音乐的APP,这样就可以让程序后台进行相关操作了,具体做法如下: 1:在Ap ...

  8. Android实训案例(六)——四大组件之一BroadcastReceiver的基本使用,拨号,短信,SD卡,开机,应用安装卸载监听

    Android实训案例(六)--四大组件之一BroadcastReceiver的基本使用,拨号,短信,SD卡,开机,应用安装卸载监听 Android中四大组件的使用时重中之重,我这个阶段也不奢望能把他 ...

  9. asp.net core启动源码以及监听,到处理请求响应的过程

    摘要 asp.net core发布至今已经将近6年了,很多人对于这一块还是有些陌生,或者说没接触过:接触过的,对于asp.net core整个启动过程,监听过程,以及请求过程,响应过程也是一知半解,可 ...

随机推荐

  1. Windows Server 笔记(七):Windows Server 2012 R2 NIC Teaming(NIC组)

    什么是NIC Teaming?         NIC Teaming 就是将两个或更多的网络适配器组合在一起,从而达到容错和带宽聚合作用.NIC Teaming 中的每个网络适配器都是物理存在的(虚 ...

  2. AppDOMain(摘录)

    AppDomain是CLR的运行单元,它可以加载Assembly.创建对象以及执行程序. AppDomain是CLR实现 代码隔离 的基本机制. 每一个AppDomain可以单独运行.停止:每个App ...

  3. 使用 Region,RegionManager 在 XNA 中创建特殊区域(十八)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  4. python2.7运行报警告:UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal解决办法

    1. 程序源代码报错部分: #选择年级if grade == '幼升小': outline.nianji().pop(0).click()elif grade == "一年级": ...

  5. python学习-- django 2.1.7 ajax 请求 进阶版

    #原来版本 $.get("/add/",{'a':a,'b':b}, function(ret){ $('#result').html(ret)}) #进阶版  $.get(&qu ...

  6. Leetcode 581.最短无序连续子数组

    最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, ...

  7. Map 中的EntrySet() ,Map的遍历

      我们循环Map时一般用到EntrySet(),EntrySet() 返回的时Set集合(Set<Map.Entry<K, V>>). 那么这里的有Map.Entry< ...

  8. BZOJ 1083:[SCOI2005]繁忙的都市(最小生成树)

    1083: [SCOI2005]繁忙的都市 Description 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路 ...

  9. 座位安排(seat)

    座位安排(seat) 题目描述 费了一番口舌,wfj_2048终于成功地说服了n∗mn∗m个妹子来陪自己看电影. 为了这次声势浩大,wfj_2048包下了一座有n∗mn∗m个座位的电影院. (wfj_ ...

  10. cmake 版本升级

    1.在网址 https://cmake.org/files/v3.1/下载   cmake-3.1.0.tar.gz 2.解压 3.执行  ./configure 4.执行 make 5. 执行   ...