CaptainHook Hook 代码

// 无参数
CHDeclareClass(WCActionSheet)
CHOptimizedMethod0(self, NSArray *, WCActionSheet, buttonTitleList){
    NSArray *titles = CHSuper0(WCActionSheet, buttonTitleList);
    return titles;
}

CHConstructor{
    CHLoadLateClass(WCActionSheet);
    CHClassHook0(WCActionSheet, buttonTitleList);
}
// 一个参数
CHDeclareClass(MMUIImageView);
CHOptimizedMethod1(self, void, MMUIImageView, setImage, NSString *, imageurl){
    NSLog(@"setImage: %@", imageurl);
    CHSuper1(MMUIImageView, setImage, imageurl);
}
CHConstructor{    
    CHLoadLateClass(MMUIImageView);
    CHClassHook1(MMUIImageView, setImage);
}

例子:

@interface Lefex : NSObject
@property (nonatomic, copy) NSString *city;
+ (Lefex *)empty;
- (void)updateNickName:(NSString *)nickName;
- (void)updateNickName:(NSString *)nickName age:(int)age;
- (void)requestNickNameForId:(NSString *)userId completion:(void(^)(NSString *))block;
@end
@synthesize city = _city;
@implementation Lefex

+ (Lefex *)empty {
    NSLog(@"orgin - %@", NSStringFromSelector(_cmd));
    return [Lefex new];
}

- (void)updateNickName:(NSString *)nickName {
    NSLog(@"orgin - %@", NSStringFromSelector(_cmd));
}

- (void)updateNickName:(NSString *)nickName age:(int)age {
    NSLog(@"orgin - %@", NSStringFromSelector(_cmd));
}

- (void)requestNickNameForId:(NSString *)userId completion:(void(^)(NSString *))block {
    NSLog(@"orgin - %@", NSStringFromSelector(_cmd));
    if (block) {
        block(@"lefe_x");
    }
}

- (void)setCity:(NSString *)city {
    _city = city;
}

- (NSString *)city {
    return _city;
}
@end
typedef void(^RequestBlock)(NSString *);

// hook某个类时需要先声明
CHDeclareClass(Lefex)

// Hook 类方法
CHOptimizedClassMethod0(self, Lefex *, Lefex, empty){
    Lefex *me = CHSuper0(Lefex, empty);
    NSLog(@"Hook empty");
    return me;
}

// Hook 一个参数的方法
CHOptimizedMethod1(self, void, Lefex, updateNickName, NSString *, name) {
    CHSuper1(Lefex, updateNickName, name);
    NSLog(@"hook updateNickName");
}

// Hook属性,hook对应的get和set方法
// hook set 方法
CHOptimizedMethod1(self, void, Lefex, setCity, NSString *, city) {
    CHSuper1(Lefex, setCity, @"BeiJing");
    NSLog(@"hook setCity");
}

// hook get 方法
CHOptimizedMethod0(self, void, Lefex, city) {
    NSLog(@"hook city");
    return CHSuper0(Lefex, city);
}

// Hook 两个参数的方法
CHOptimizedMethod2(self, void, Lefex, updateNickName, NSString *, name, age, int, age) {
    CHSuper2(Lefex, updateNickName, name, age, age);
    NSLog(@"hook updateNickName: age");
}

// Hook 带有block的方法,需要知道 block 中的参数个数
CHOptimizedMethod2(self, void, Lefex, requestNickNameForId, NSString *, userId, completion, RequestBlock, block) {
    RequestBlock nicknameBlock = ^(NSString *nickname) {
        NSLog(@"hook callback :%@", nickname);
        if (block) {
            block(nickname);
        }
    };
    
    CHSuper2(Lefex, requestNickNameForId, userId, completion, nicknameBlock);
    NSLog(@"hook requestNickNameForId: age");
}

// Hook 参数是二级指针的方法
CHOptimizedMethod1(self, void, Lefex, queryLocation, NSString **, name) {
    CHSuper1(Lefex, queryLocation, name);
    NSLog(@"hook queryLocation: %@", *name);
}

CHConstructor {
    // 导入类才能够使用
    // linkable
    CHLoadClass(Lefex);
    // un linkable
//    CHLoadLateClass(Lefex);

    CHClassHook1(Lefex, setCity);
    CHClassHook0(Lefex, city);
    CHClassHook0(Lefex, empty);
    CHClassHook1(Lefex, updateNickName);
    CHClassHook2(Lefex, updateNickName, age);
    CHClassHook2(Lefex, requestNickNameForId, completion);
    CHClassHook1(Lefex, queryLocation);
}

你可能感兴趣的:(CaptainHook Hook 代码)