CaptainHook使用

- (bool)test1 {
    NSLog(@"%s",__func__);
    return true;
}
+ (bool)test2 {
    NSLog(@"%s",__func__);
    return true;
}

CHDeclareClass(ViewController)
CHMethod0(bool, ViewController, test1) {
    NSLog(@"%s",__func__);
    return false;
}
CHClassMethod0(bool, ViewController, test2) {
    NSLog(@"%s",__func__);
    return false;
}
// 类必须用CHClassHook和CHClassMethod
+ (void)load {
    CHLoadLateClass(ViewController);
    CHHook0(ViewController, test1);
    CHClassHook0(ViewController, test2);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"test1:%d,test2:%d", [self test1], [ViewController test2]);
}
// 打印:
$ViewController_test1_method
$ViewController_test2_method
test1:0,test2:0

你可能感兴趣的:(CaptainHook使用)