iOS【方法拦截替换的思路】

2020-06-02  本文已影响0人  NJ_墨

摘录:「想名真难」

记录一下几个方法,思路

第一种方式:
category添加方法,然后做交换

+ (void)load {
    SEL origSel = @selector(setText:) ;
    SEL altSel = @selector(setTextHooked:) ;
 
    Method origMethod = class_getInstanceMethod(self, origSel);
    Method altMethod = class_getInstanceMethod(self, altSel);
    if (!origMethod || !altMethod) {
        return ;
    }
    // 交换实现
    method_exchangeImplementations(origMethod,altMethod);
    
}
/// 主要是解决 服务器有时返回NSNumber类型,但是用了NSString指针接收,在 label.text = @(num) 时崩溃
- (void) setTextHooked:(NSString *)string {
    
    if (string == nil) {
        string = @"";
    }
    if ([string isKindOfClass:[NSNumber class]]) {
        string = string.description;
    }
    
    if ([string isKindOfClass:[NSString class]] && [string containsString:@"(null)"]) {
        string = [string stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
    }
    
    [self setTextHooked:string.description] ;
    
}

第二种方式:
在外界给UILabel添加一个方法。

#import "OhterHook2.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import <UIKit/UIKit.h>
 
@implementation OhterHook2
 
+ (void)load {
    
    SEL origSel = @selector(setText:) ;
    SEL altSel = @selector(setTextHooked:) ;
 
    Method origMethod = class_getInstanceMethod([UILabel class], origSel);
    // 给UILabel添加一个方法,然后在交换.不需要在类别中实现
    class_addMethod([UILabel class],altSel,
                    (IMP)setTextHooked,
                    "v@:@");
    Method altMethod = class_getInstanceMethod([UILabel class], altSel);
    method_exchangeImplementations(origMethod,altMethod);
    
}
 
void setTextHooked(id self , SEL _cmd,NSString * string) {
    ...
    // 这里的self是UILabel,_cmd是setText:
    objc_msgSend(self, @selector(setTextHooked:),string.description);
    
}
/// 没用,只是为了消除警告
- (void) setTextHooked:(NSString *)string {}
@end

第三种方式:
获取到原始方法的imp,通过imp调用原始方法,完成设置text.

#import "OtherHook.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import <UIKit/UIKit.h>
@implementation OtherHook
 
+ (void)load {
    
    SEL origSel = @selector(setText:) ;
    SEL altSel = @selector(setTextHooked:) ;
    
    Method origMethod = class_getInstanceMethod([UILabel class], origSel);
    Method altMethod = class_getInstanceMethod(self, altSel);
    if (!origMethod || !altMethod) {
        return ;
    }
    method_exchangeImplementations(origMethod,altMethod);
    
}
 
- (void) setTextHooked:(NSString *)string {
    ......
 
    // 这里的self是UILabel,_cmd是setText:
    // 这样不行,因为UILabel中没有这个setTextHooked:,所以会找不到方法崩溃
    //    [self setTextHooked:string.description] ;
    // 这也不行,会递归
//    objc_msgSend(self, @selector(setText:),string.description);
    
    // 多次尝试这个才行,获取到原始方法的imp,通过imp调用原始方法,完成设置text
    Method altMethod = class_getInstanceMethod([OtherHook class], @selector(setTextHooked:));
    IMP origImp = method_getImplementation(altMethod);
    origImp(self,_cmd,string.description);
    
    
}
 
 
@end

第四种方式:

可以使用category在类别中覆盖同样的方法,然后其他在调用setText的时候会优先调用到category中的方法,然后在category中处理完成之后再调用系统的setText方法.

@implementation UILabel (SafeSetText)

- (void)setText:(NSString *)text {
    
    .....
    // 调用系统的原始方法,
    // 1.从方法列表中倒序寻找,类别中的方法在最前面,如果正序寻找需要遍历完,倒序可以break
    // 2.找到第一个匹配的就是系统的原始方法,
    // 3.用原始方法的imp指针调用
    u_int count = 0;
    // class_copyMethodList只会找本类的方法,如果是UILabel的子类会找不到而崩溃,所以这里必须是[UILabel class]
    Method * methods = class_copyMethodList([UILabel class], &count);
    NSInteger index = -1;
    for (int i = count-1; i>=0; i--) {
        SEL tempSel = method_getName(methods[i]);
        NSString * strName = [NSString stringWithCString:sel_getName(tempSel) encoding:NSUTF8StringEncoding];
        NSLog(@"%d  %@",i,strName);
        if (tempSel == _cmd) {
            index = i;
            break;
        }
        
    }
 
    NSAssert(index != -1, @"未找到指定的方法");
 
    SEL resultSel = method_getName(methods[index]);
    IMP imp = method_getImplementation(methods[index]);
    
    ( (void (*) (id,SEL,NSString *)) imp)(self,resultSel,text);
    
}
 
@end
上一篇 下一篇

猜你喜欢

热点阅读