1、导入头文件
#import "ViewController.h"
#import
@interface ViewController ()
@end
声明静态timer 防止销毁
static dispatch_source_t timer;
@implementation ViewController
//检测是否被调试
BOOL isDebugger() {
//控制码
int name[4];//里面放字节码,查询信息
name[0] =CTL_KERN;//内核查看
name[1] =KERN_PROC;//查询进程
name[2] = KERN_PROC_PID;//传递的参数是进程的ID(PID)
name[3] =getpid();//PID的值
struct kinfo_proc info; //接受进程查询结果的结构体
size_tinfo_size =sizeof(info);//结构体的大小
interror =sysctl(name,sizeof(name)/sizeof(*name), &info, &info_size,0,0);
assert(error == 0);//0就是没有错误,其他就是错误码
return((info.kp_proc.p_flag&P_TRACED) !=0);
}
void debugCheck(){
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0.0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
if(isDebugger()) {
// exit(0);
NSLog(@"有debugServer");
}else{
NSLog(@"无debugServer");
}
});
dispatch_resume(timer);
}
//在viewDidLoad 的调用
- (void)viewDidLoad {
[super viewDidLoad];
debugCheck();
}