Giter Site home page Giter Site logo

zdffihook's Introduction

ZDFfiHook

使用libffi实现hook Objective-C

原理:

libffi根据Method动态生成一个新的NewIMP,接着libffi把这个NewIMP与一个C函数做关联,然后通过OC的方法交换(这里需要保存原始的IMP)把MethodIMP替换为libffi生成的NewIMP,这样当OC方法执行时会走到关联的C函数中,在这里我们可以控制原OC方法的执行时机来达到hook的目的。

用法

回调的callback参数列表与实际被hook的参数(不包括selfselector)一一对应,不带参数的方法 callback参数列表为void

API:

/// hook 实例方法
+ (ZDFfiHookInfo *)zd_hookInstanceMethod:(SEL)selector option:(ZDHookOption)option callback:(id)callback;

/// hook类方法
+ (ZDFfiHookInfo *)zd_hookClassMethod:(SEL)selector option:(ZDHookOption)option callback:(id)callback;

/// 仅仅对单个实例对象进行hook
- (ZDFfiHookInfo *)zd_hookInstanceMethod:(SEL)selector option:(ZDHookOption)option callback:(id)callback;

/// 移除hook
+ (BOOL)zd_removeHookToken:(ZDFfiHookInfo *)token;

/// 一般情况下不用手动移除单个实例的hook,当实例释放时会自动移除
- (BOOL)zd_removeHookToken:(ZDFfiHookInfo *)token;

例子

- (void)testFfiHook {
    [self.class zd_hookInstanceMethod:@selector(exeA:b:c:) option:ZDHookOption_After callback:^(NSInteger a, NSString *b, id c){
        NSLog(@"~~~~~后hook");
    }];

    [self.class zd_hookInstanceMethod:@selector(exeA:b:c:) option:ZDHookOption_Before callback:^(NSInteger a, NSString *b, id c){
        NSLog(@"~~~~~~先hook");
    }];

    id v = [self exeA:100 b:@"啦啦啦" c:NSObject.new];
    NSLog(@"***************** %@", v);
}

#pragma mark - Method

- (id)exeA:(NSInteger)a b:(NSString *)b c:(id)c {
    NSString *ret = [NSString stringWithFormat:@"结果 = %zd%@%@", a, b, c];
    return ret;
}

安装

pod 'ZDFfiHook', :git => 'https://github.com/faimin/ZDFfiHook.git'

参考

zdffihook's People

Contributors

faimin avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.