iOS利用runtime追踪对象的每一个方法

更新时间:2017-01-21 10:59:15 点击次数:1871次

iOS利用runtime追踪对象的每一个方法

我们会用到runtime替换方法来监听某个方法的调用。例如,项目中每个Controller都直接继承了UIViewController,但是现在想监听每个Controller的viewDidAppear 和 viewDidDisappear,用法如下:

void qhd_exchangeInstanceMethod(Class class, SEL originalSelector, SEL newSelector) { Method originalMethod = class_getInstanceMethod(class, originalSelector); Method newMethod = class_getInstanceMethod(class, newSelector); if(class_addMethod(class, originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) { class_replaceMethod(class, newSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else {
        method_exchangeImplementations(originalMethod, newMethod);
    }
} @implementation UIViewController (Test)

+ (void)load {
    qhd_exchangeInstanceMethod([self class], @selector(viewDidAppear:), @selector(qhd_viewDidAppear:)); qhd_exchangeInstanceMethod([self class], @selector(viewDidDisappear:), @selector(qhd_viewDidDisappear:)); }

- (void)qhd_viewDidAppear:(BOOL)animated { //[MobClick beginLogPageView:self.title];
    [self qhd_viewDidAppear:animated];
}

- (void)qhd_viewDidDisappear:(BOOL)animated { //[MobClick endLogPageView:self.title];
    [self qhd_viewDidDisappear:animated];
} @end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

近产生了一个的想法:替换一个类的所有方法,每一个方法都打印一个log,看看调用顺序是怎样的,例如我想知道UIViewController在运行时到底都调用了哪些方法,包括私有方法。

思路是这样的: 
1.通过class_copyMethodList得出一个类的所有方法。 
2.通过method_getTypeEncoding和method_copyReturnType得出方法的参数类型和返回值。 
3.创建出SEL和IMP,通过class_addMethod动态添加新方法。 
4.通过交换的思想,在新方法里通过NSInvocation来调用原方法。

难点在于,新方法里面怎么把方法的“实现”(即IMP)绑定上,并且在“实现”里调用原方法。在runtime的头文件中Method的结构:

typedef struct objc_method *Method; struct objc_method {
    SEL method_name            
    char *method_types         
    IMP method_imp               
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

可以看到Method包含了是三个元素:一个SEL,一个char *,一个IMP。 
SEL是方法名,char *是方法的类型,IMP就是实现的地址。

本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

回到顶部
嘿,我来帮您!