使用runtime替换类的方法

2018-08-31  本文已影响0人  William__Lu
使用runtime替换类的方法
一些系统类或者第三方库的类方法无法修改原文件中的方法,可以通过添加分类,method_exchangeImplementations替换方法实现对原方法的修改
#import "NSObject+Repail.h"
#import <objc/runtime.h>
#import "LivefaceViewController.h"
@implementation LivefaceViewController (Repail)

+ (void)load {
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    Method m1 = class_getInstanceMethod([self class], NSSelectorFromString(@"setCallBackQueue:"));
    Method m2 = class_getInstanceMethod([self class], NSSelectorFromString(@"__t_setCallBackQueue:"));
    method_exchangeImplementations(m1, m2);
  });
}

- (void)__t_setCallBackQueue:(dispatch_queue_t)callBackQueue {
  [self performSelector:@selector(__t_setCallBackQueue:) withObject:dispatch_get_main_queue() afterDelay:0];
}

@end
上一篇下一篇

猜你喜欢

热点阅读