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

IOS7 专栏收录该内容
12 篇文章 0 订阅

非越狱情况下实现:

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

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

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


配置项目 plist文件

添加:

<key>UIBackgroundModes</key>

<array>

<string>voip</string>

</array>


功能类:ProccessHelper

#import <Foundation/Foundation.h>  
  
@interface ProccessHelper : NSObject  
  
+ (NSArray *)runningProcesses;  
  
@end  

[cpp] view plaincopyprint?
#import "ProccessHelper.h"  
//#include<objc/runtime.h>  
#include <sys/sysctl.h>  
  
#include <stdbool.h>  
#include <sys/types.h>  
#include <unistd.h>  
#include <sys/sysctl.h>  
  
@implementation ProccessHelper  
  
//You can determine if your app is being run under the debugger with the following code from  
static bool AmIBeingDebugged(void)  
// Returns true if the current process is being debugged (either  
// running under the debugger or has a debugger attached post facto).  
{  
    int                 junk;  
    int                 mib[4];  
    struct kinfo_proc   info;  
    size_t              size;  
      
    // Initialize the flags so that, if sysctl fails for some bizarre  
    // reason, we get a predictable result.  
      
    info.kp_proc.p_flag = 0;  
      
    // Initialize mib, which tells sysctl the info we want, in this case  
    // we're looking for information about a specific process ID.  
      
    mib[0] = CTL_KERN;  
    mib[1] = KERN_PROC;  
    mib[2] = KERN_PROC_PID;  
    mib[3] = getpid();  
      
    // Call sysctl.  
      
    size = sizeof(info);  
    junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);  
    assert(junk == 0);  
      
    // We're being debugged if the P_TRACED flag is set.  
      
    return ( (info.kp_proc.p_flag & P_TRACED) != 0 );  
}  
  
//返回所有正在运行的进程的 id,name,占用cpu,运行时间  
//使用函数int   sysctl(int *, u_int, void *, size_t *, void *, size_t)  
+ (NSArray *)runningProcesses  
{  
    //指定名字参数,按照顺序第一个元素指定本请求定向到内核的哪个子系统,第二个及其后元素依次细化指定该系统的某个部分。  
    //CTL_KERN,KERN_PROC,KERN_PROC_ALL 正在运行的所有进程  
    int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL ,0};  
      
      
    size_t miblen = 4;  
    //值-结果参数:函数被调用时,size指向的值指定该缓冲区的大小;函数返回时,该值给出内核存放在该缓冲区中的数据量  
    //如果这个缓冲不够大,函数就返回ENOMEM错误  
    size_t size;  
    //返回0,成功;返回-1,失败  
    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);  
      
    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--)  
                {  
                    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
                    NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];  
                    NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];  
                    NSString * proc_CPU = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_estcpu];  
                    double t = [[NSDate date] timeIntervalSince1970] - process[i].kp_proc.p_un.__p_starttime.tv_sec;  
                    NSString * proc_useTiem = [[NSString alloc] initWithFormat:@"%f",t];  
                    NSString *startTime = [[NSString alloc] initWithFormat:@"%ld", process[i].kp_proc.p_un.__p_starttime.tv_sec];  
                    NSString * status = [[NSString alloc] initWithFormat:@"%d",process[i].kp_proc.p_flag];  
                      
                    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];  
                    [dic setValue:processID forKey:@"ProcessID"];  
                    [dic setValue:processName forKey:@"ProcessName"];  
                    [dic setValue:proc_CPU forKey:@"ProcessCPU"];  
                    [dic setValue:proc_useTiem forKey:@"ProcessUseTime"];  
                    [dic setValue:proc_useTiem forKey:@"ProcessUseTime"];  
                    [dic setValue:startTime forKey:@"startTime"];  
                      
                    // 18432 is the currently running application  
                    // 16384 is background  
                    [dic setValue:status forKey:@"status"];  
                      
                    [processID release];  
                    [processName release];  
                    [proc_CPU release];  
                    [proc_useTiem release];  
                    [array addObject:dic];  
                    [startTime release];  
                    [status release];  
                    [dic release];  
                      
                    [pool release];  
                }  
                  
                free(process);  
                process = NULL;  
                //NSLog(@"array = %@",array);  
                  
                return array;  
            }  
        }  
    }  
      
    return nil;  
}  
  
@end  

实现代码:

systemprocessArray = [[NSMutableArray arrayWithObjects:  
                                @"kernel_task",  
                                @"launchd",  
                                @"UserEventAgent",  
                                @"wifid",  
                                @"syslogd",  
                                @"powerd",  
                                @"lockdownd",  
                                @"mediaserverd",  
                                @"mediaremoted",  
                                @"mDNSResponder",  
                                @"locationd",  
                                @"imagent",  
                                @"iapd",  
                                @"fseventsd",  
                                @"fairplayd.N81",  
                                @"configd",  
                                @"apsd",  
                                @"aggregated",  
                                @"SpringBoard",  
                                @"CommCenterClassi",  
                                @"BTServer",  
                                @"notifyd",  
                                @"MobilePhone",  
                                @"ptpd",  
                                @"afcd",  
                                @"notification_pro",  
                                @"notification_pro",  
                                @"syslog_relay",  
                                @"notification_pro",  
                                @"springboardservi",  
                                @"atc",  
                                @"sandboxd",  
                                @"networkd",  
                                @"lsd",  
                                @"securityd",  
                                @"lockbot",  
                                @"installd",  
                                @"debugserver",  
                                @"amfid",  
                                @"AppleIDAuthAgent",  
                                @"BootLaunch",  
                                @"MobileMail",  
                                @"BlueTool",  
                                nil] retain];  

- (void)applicationDidEnterBackground:(UIApplication *)application  
{  
    while (1) {  
        sleep(5);  
        [self postMsg];  
    }  
      
[cpp] view plaincopyprint?
    [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{  
        NSLog(@"KeepAlive");  
    }];  
}  
  
- (void)applicationWillResignActive:(UIApplication *)application  
{  
}  
- (void)applicationWillEnterForeground:(UIApplication *)application  
{  
}  
- (void)applicationDidBecomeActive:(UIApplication *)application  
{  
}  
- (void)applicationWillTerminate:(UIApplication *)application  
{  
}  
  
#pragma mark -  
#pragma mark - User Method  
  
- (void) postMsg  
{  
    //上传到服务器  
    NSURL *url = [self getURL];  
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];  
    NSError *error = nil;  
    NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];  
      
    if (error) {  
        NSLog(@"error:%@", [error localizedDescription]);  
    }  
      
    NSString *str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];  
    NSLog(@"%@",str);  
}  
  
- (NSURL *) getURL  
{  
    UIDevice *device = [UIDevice currentDevice];  
      
    NSString* uuid = @"TESTUUID";  
    NSString* manufacturer = @"apple";  
    NSString* model = [device model];  
    NSString* mobile = [device systemVersion];  
      
    NSString *msg = [NSString stringWithFormat:@"Msg:%@  Time:%@", [self processMsg], [self getTime]];  
    CFShow(msg);  
      
    /  省略部分代码  /  
      
    NSString *urlStr = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
    NSURL *url = [NSURL URLWithString:urlStr];  
      
    return url;  
}  
  
- (BOOL) checkSystemProccess:(NSString *) proName  
{  
    if ([systemprocessArray containsObject:proName]) {  
        return YES;  
    }  
    return NO;  
}  
  
- (BOOL) checkFirst:(NSString *) string  
{  
    NSString *str = [string substringToIndex:1];  
    NSRange r = [@"ABCDEFGHIJKLMNOPQRSTUVWXWZ" rangeOfString:str];  
      
    if (r.length > 0) {  
        return YES;  
    }  
    return NO;  
}  
  
- (NSString *) processMsg  
{  
    NSArray *proMsg = [ProccessHelper runningProcesses];  
  
    if (proMsg == nil) {  
        return nil;  
    }  
      
    NSMutableArray *proState = [NSMutableArray array];  
    for (NSDictionary *dic in proMsg) {  
          
        NSString *proName = [dic objectForKey:@"ProcessName"];  
        if (![self checkSystemProccess:proName] && [self checkFirst:proName]) {  
            NSString *proID = [dic objectForKey:@"ProcessID"];  
            NSString *proStartTime = [dic objectForKey:@"startTime"];  
              
            if ([[dic objectForKey:@"status"] isEqualToString:@"18432"]) {  
                NSString *msg = [NSString stringWithFormat:@"ProcessName:%@ - ProcessID:%@ - StartTime:%@ Running:YES", proName, proID, proStartTime];  
                [proState addObject:msg];  
            } else {  
                NSString *msg = [NSString stringWithFormat:@"ProcessName:%@ - ProcessID:%@ - StartTime:%@ Running:NO", proName, proID, proStartTime];  
                [proState addObject:msg];  
            }  
        }  
    }  
      
    NSString *msg = [proState componentsJoinedByString:@"______"];  
    return msg;  
}  
  
// 获取时间  
- (NSString *) getTime  
{  
    NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];  
    formatter.dateStyle = NSDateFormatterMediumStyle;  
    formatter.timeStyle = NSDateFormatterMediumStyle;  
    formatter.locale = [NSLocale currentLocale];  
      
    NSDate *date = [NSDate date];  
      
    [formatter setTimeStyle:NSDateFormatterMediumStyle];  
    NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];  
    NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];  
    NSInteger unitFlags = NSYearCalendarUnit |  
    NSMonthCalendarUnit |  
    NSDayCalendarUnit |  
    NSWeekdayCalendarUnit |  
    NSHourCalendarUnit |  
    NSMinuteCalendarUnit |  
    NSSecondCalendarUnit;  
    comps = [calendar components:unitFlags fromDate:date];  
    int year = [comps year];  
    int month = [comps month];  
    int day = [comps day];  
    int hour = [comps hour];  
    int min = [comps minute];  
    int sec = [comps second];  
      
    NSString *time = [NSString stringWithFormat:@"%d-%d-%d %d:%d:%d", year, month, day, hour, min, sec];  
     
    return time;  
}  
  
@end  


  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
&lt;p style=&quot;font-size:16px;color:#666666;&quot;&gt; &lt;img src=&quot;https://img-bss.csdn.net/202001311426171105.png&quot; alt=&quot;&quot; /&gt; &lt;/p&gt; &lt;p style=&quot;font-size:16px;color:#666666;&quot;&gt; &lt;strong&gt;&lt;span style=&quot;font-size:20px;&quot;&gt;课程目标&lt;/span&gt;&lt;/strong&gt; &lt;/p&gt; &lt;p style=&quot;font-size:16px;color:#666666;&quot;&gt; 《从零开始学Scrapy网络爬虫》从零开始,循序渐进地介绍了目前流行的网络爬虫框架Scrapy。即使你没有任何编程基础,学习起来也不会有压力,因为我们有针对性地介绍了Python编程技术。另外,《从零开始学Scrapy网络爬虫》在讲解过程中以案例为导向,通过对案例的不断迭代、优化,让读者加深对知识的理解,并通过14个项目案例,提高学习者解决实际问题的能力。 &lt;/p&gt; &lt;p style=&quot;font-size:16px;color:#666666;&quot;&gt; &lt;br /&gt; &lt;/p&gt; &lt;p style=&quot;font-size:16px;color:#666666;&quot;&gt; &lt;strong&gt;&lt;span style=&quot;font-size:20px;&quot;&gt;适合对象&lt;/span&gt;&lt;/strong&gt; &lt;/p&gt; &lt;p style=&quot;font-size:16px;color:#666666;&quot;&gt; 爬虫初学者、爬虫爱好者、高校相关专业的学生、数据爬虫工程师。 &lt;/p&gt; &lt;p style=&quot;font-size:16px;color:#666666;&quot;&gt; &lt;br /&gt; &lt;/p&gt; &lt;p style=&quot;font-size:16px;color:#666666;&quot;&gt; &lt;span style=&quot;font-size:20px;&quot;&gt;&lt;strong&gt;课程介绍&lt;/strong&gt;&lt;/span&gt; &lt;/p&gt; &lt;p style=&quot;font-size:16px;color:#666666;&quot;&gt; 《从零开始学Scrapy网络爬虫》共13章。其中,第14章为基础篇,介绍了Python基础、网络爬虫基础、Scrapy框架及基本的爬虫功能。第510章为进阶篇,介绍了如何将爬虫数据存储于MySQL、MongoDB和Redis数据库中;如何实现异步AJAX数据的爬取;如何使用Selenium和Splash实现动态网站的爬取;如何实现模拟登录功能;如何突破反爬虫技术,以及如何实现文件和图片的下载。第11~13章为高级篇,介绍了使用Scrapy-Redis实现分布式爬虫;使用Scrapyd和Docker部署分布式爬虫;使用Gerapy管理分布式爬虫,并实现了一个抢票软件的综合项目。 &lt;/p&gt; &lt;p style=&quot;font-size:16px;color:#666666;&quot;&gt; &lt;span style=&quot;color:#FF0000;&quot;&gt;&nbsp; &nbsp; &nbsp; 由于目标网站可能会对页面进行改版或者升级反爬虫措施,如果发现视频中的方法无法成功爬取数据,敬请按照页面实际情况修改XPath的路径表达式。视频教程主要提供理论、方法支撑。我们也会在第一时间更新源代码,谢谢!&lt;/span&gt; &lt;/p&gt; &lt;p style=&quot;font-size:16px;color:#666666;&quot;&gt; &lt;img src=&quot;https://img-bss.csdn.net/202001311426306665.png&quot; alt=&quot;&quot; /&gt; &lt;/p&gt; &lt;p style=&quot;font-size:16px;color:#666666;&quot;&gt; &lt;strong&gt;&lt;span style=&quot;font-size:20px;&quot;&gt;课程特色&lt;/span&gt;&lt;/strong&gt; &lt;/p&gt; &lt;p style=&quot;font-size:16px;&quot;&gt; &lt;img src=&quot;https://img-bss.csdn.net/202001311426415123.png&quot; alt=&quot;&quot; /&gt; &lt;/p&gt; &lt;div&gt; &lt;br /&gt; &lt;/div&gt;
&lt;p&gt; YOLOv3是一种基于深度学习的端到端实时目标检测方法,以速度快见长。本课程将手把手地教大家使用labelImg标注和使用YOLOv3训练自己的数据集。课程分为三个小项目:足球目标检测(单目标检测)、梅西目标检测(单目标检测)、足球和梅西同时目标检测(两目标检测)。&lt;br /&gt; &lt;br /&gt; 本课程的YOLOv3使用Darknet,在Ubuntu系统上做项目演示。包括:安装Darknet、给自己的数据集打标签、整理自己的数据集、修改配置文件、训练自己的数据集、测试训练出的网络模型、性能统计(mAP计算和画出PR曲线)和先验框聚类。&lt;br /&gt; &lt;br /&gt; Darknet是使用C语言实现的轻型开源深度学习框架,依赖少,可移植性好,值得深入探究。 &lt;/p&gt; &lt;p&gt; 购课后可加入白勇老师课程学习交流QQ群:957519975&lt;br /&gt; &lt;br /&gt; 除本课程《YOLOv3目标检测实战:训练自己的数据集》外,本人推出了有关YOLOv3目标检测的系列课程,请持续关注该系列的其它课程视频,包括:&lt;br /&gt; &lt;br /&gt; 《YOLOv3目标检测实战:交通标志识别》&lt;br /&gt; &lt;br /&gt; 《YOLOv3目标检测:原理与源码解析》&lt;br /&gt; &lt;br /&gt; 《YOLOv3目标检测:网络模型改进方法》 &lt;/p&gt; &lt;p&gt; 敬请关注并选择学习! &lt;/p&gt; &lt;p&gt; &lt;img alt=&quot;&quot; src=&quot;https://img-bss.csdn.net/201905290055196890.jpg&quot; /&gt; &lt;/p&gt;
&lt;p&gt; &lt;span style=&quot;font-size:16px;&quot;&gt;本课程从零开始,以通俗易懂的方式讲解Git技术,手把手教你掌握每一个知识点。&lt;/span&gt; &lt;/p&gt; &lt;p&gt; &lt;span style=&quot;color:#FF0000;font-size:16px;&quot;&gt;&lt;br /&gt; &lt;/span&gt; &lt;/p&gt; &lt;p&gt; &lt;span style=&quot;font-size:16px;&quot;&gt;课程内容包括:&lt;/span&gt;&lt;span style=&quot;font-size:16px;&quot;&gt;版本控制系统、&lt;/span&gt;&lt;span style=&quot;font-size:16px;&quot;&gt;Git简介、&lt;/span&gt;&lt;span style=&quot;font-size:16px;&quot;&gt;Git使用、常用命令、&lt;/span&gt;&lt;span style=&quot;font-size:16px;&quot;&gt;远程仓库、&lt;/span&gt;&lt;span style=&quot;font-size:16px;&quot;&gt;分支Branch、&lt;/span&gt;&lt;span style=&quot;font-size:16px;&quot;&gt;静态页面托管、&lt;/span&gt;&lt;span style=&quot;font-size:16px;&quot;&gt;在IDEA中使用Git&lt;/span&gt; &lt;/p&gt; &lt;p&gt; &lt;br /&gt; &lt;/p&gt; &lt;p&gt; &lt;span style=&quot;font-size:16px;&quot;&gt;&lt;strong&gt;&lt;span style=&quot;color:#FF0000;&quot;&gt;教学全程采用笔记+代码案例的形式讲解,通俗易懂&lt;/span&gt;&lt;/strong&gt;&lt;strong&gt;&lt;span style=&quot;color:#FF0000;&quot;&gt;!!!&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt; &lt;/p&gt; &lt;p&gt; &lt;br /&gt; &lt;/p&gt; &lt;p&gt; &lt;img src=&quot;https://img-bss.csdn.net/201910230828556919.png&quot; alt=&quot;&quot; /&gt; &lt;/p&gt; &lt;p&gt; &lt;br /&gt; &lt;/p&gt; &lt;p&gt; &lt;img src=&quot;https://img-bss.csdn.net/201910230829097767.png&quot; alt=&quot;&quot; /&gt; &lt;/p&gt; &lt;p&gt; &lt;br /&gt; &lt;/p&gt; &lt;p&gt; &lt;img src=&quot;https://img-bss.csdn.net/201910230829185636.png&quot; alt=&quot;&quot; /&gt; &lt;/p&gt; &lt;p&gt; &lt;span style=&quot;font-size:16px;&quot;&gt;&lt;strong&gt;&lt;br /&gt; &lt;/strong&gt;&lt;/span&gt; &lt;/p&gt; &lt;p&gt; &lt;span style=&quot;font-size:16px;&quot;&gt;&lt;strong&gt;讲师介绍&lt;/strong&gt;&lt;/span&gt; &lt;/p&gt; &lt;p&gt; &lt;img src=&quot;https://img-bss.csdn.net/201910220910188617.jpg&quot; alt=&quot;&quot; /&gt; &lt;/p&gt;
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值