第二节 fishHook的简单使用

fishHook的结构体的介绍

struct rebinding {
const char *name;//需要hook的函数名称,字符串
void *replacement;//替换到那个心的函数上(函数指针,也就是函数的名称)
void **replaced;//保存原始函数指针变量的指针(二级指针)
};

fishHook使用步骤

1、定义结构体,结构体参考fishHook的结构体

//函数名称
nslogBind.name = "NSLog";
//新的函数地址
nslogBind.replacement = myNSLog;
//保存原始的函数地址的变量指针
nslogBind.replaced = (void *)&old_nslog;

static void (*old_nslog)(NSString *format, ...);
void myNSLog(NSString *format, ...){
      format = [format stringByAppendingString:@"\n勾上了!"];
      //调用一起的方法,也可以处理其他的逻辑
      old_nslog(format);
}

2、定义结构体的数组

struct rebinding rebs[] = {nslogBind};

3、调用fishhook方法

rebind_symbols(rebs, 1);

fishHook完整的代码:

  • (void)viewDidLoad {
    [super viewDidLoad];
    //定义结构体
    struct rebinding nslogBind;
    //函数名称
    nslogBind.name = "NSLog";
    //新的函数地址
    nslogBind.replacement = myNSLog;
    //保存原始的函数地址的变量指针
    nslogBind.replaced = (void )&old_nslog;
    //定义数组
    struct rebinding rebs[] = {nslogBind};
    /

    arg1:存放rebinding结构体的数组
    arg2:数组的长度
    /
    rebind_symbols(rebs, 1);
    }
    static void (
    old_nslog)(NSString *format, ...);
    void myNSLog(NSString *format, ...){
    format = [format stringByAppendingString:@"\n勾上了!"];
    //调用一起的方法,也可以处理其他的逻辑
    old_nslog(format);
    }
    - (void)touchesBegan:(NSSet *)touches withEvent: (UIEvent *)event{
    NSLog(@"点击了屏幕");
    [super touchesBegan:touches withEvent:event];
    }

你可能感兴趣的:(第二节 fishHook的简单使用)