美文网首页
2019-04-10 runtime之埋点

2019-04-10 runtime之埋点

作者: zxh123456 | 来源:发表于2019-04-10 09:14 被阅读0次

需求:项目完成后,boss要统计所有界面的停留时间。
使用runtime能方便处理
思路:所有控制器继承自UIViewController,给UIViewController写个分类,重写load方法,用runtime的method_exchangeImplementations交换系统生命周期方法。

@implementation UIViewController (zxh)
+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method m1 = class_getInstanceMethod(self, NSSelectorFromString(@"zxh_viewDidAppear:"));
        Method m2 = class_getInstanceMethod(self, NSSelectorFromString(@"viewDidAppear:"));
        method_exchangeImplementations(m1, m2);

        m1 = class_getInstanceMethod(self, NSSelectorFromString(@"zxh_viewDidLoad"));
        m2 = class_getInstanceMethod(self, NSSelectorFromString(@"viewDidLoad"));
        method_exchangeImplementations(m1, m2);

        m1 = class_getInstanceMethod(self, NSSelectorFromString(@"zxh_viewWillAppear:"));
        m2 = class_getInstanceMethod(self, NSSelectorFromString(@"viewWillAppear:"));
        method_exchangeImplementations(m1, m2);

        m1 = class_getInstanceMethod(self, NSSelectorFromString(@"zxh_viewWillDisappear:"));
        m2 = class_getInstanceMethod(self, NSSelectorFromString(@"viewWillDisappear:"));
        method_exchangeImplementations(m1, m2);


        m1 = class_getInstanceMethod(self, NSSelectorFromString(@"zxh_viewDidDisappear:"));
        m2 = class_getInstanceMethod(self, NSSelectorFromString(@"viewDidDisappear:"));
        method_exchangeImplementations(m1, m2);

        m1 = class_getInstanceMethod(self, NSSelectorFromString(@"zxh_dealloc"));
        m2 = class_getInstanceMethod(self, NSSelectorFromString(@"dealloc"));
        method_exchangeImplementations(m1, m2);

        m1 = class_getInstanceMethod(self, NSSelectorFromString(@"zxh_didReceiveMemoryWarning"));
        m2 = class_getInstanceMethod(self, NSSelectorFromString(@"didReceiveMemoryWarning"));
        method_exchangeImplementations(m1, m2);

    });

}

static int loadTime = 0;

-(void)zxh_viewDidAppear:(BOOL)animated{
    NSLog(@"%@<======zxh_viewDidAppear",[self class]);
    loadTime = [NSDate timeIntervalSinceReferenceDate];
    [self zxh_viewDidAppear:animated];
}

-(void)zxh_viewDidLoad{
    NSLog(@"%@+++++++++zxh_viewDidLoad",[self class]);
    [self zxh_viewDidLoad];
}

-(void)zxh_viewWillAppear:(BOOL)animated{
    NSLog(@"%@======>zxh_viewWillAppear",[self class]);
    [self zxh_viewWillAppear:animated];
}


-(void)zxh_viewWillDisappear:(BOOL)animated{
    NSLog(@"%@<======zxh_viewWillDisappear",[self class]);
    NSLog(@"%@ 时间: %f",[self class], [NSDate timeIntervalSinceReferenceDate] - loadTime);
    [self zxh_viewWillDisappear:animated];
}

-(void)zxh_viewDidDisappear:(BOOL)animated{
    NSLog(@"%@-------zxh_viewDidDisappear",[self class]);
    [self zxh_viewDidDisappear:animated];
}

-(void)zxh_dealloc{
    NSLog(@"%@**********zxh_dealloc", [self class]);
    [self zxh_dealloc];
}


-(void)zxh_didReceiveMemoryWarning{
    NSLog(@"%@**********内存警告!!!!", [self class]);
    [self zxh_didReceiveMemoryWarning];
}

相关文章

  • 2019-04-10 runtime之埋点

    需求:项目完成后,boss要统计所有界面的停留时间。使用runtime能方便处理思路:所有控制器继承自UIView...

  • Learn Runtime

    Runtime 参考资料: RunTime应用实例--关于埋点的思考 使用Runtime进行埋点操作-Demo...

  • 埋点

    埋点:runtime运行时 hook方法viewwillappear viewwilldisappear sen...

  • iOS开发之友盟埋点

    如果只是对页面进行埋点的话,可以使用Runtime进行埋点首先写一个UIViewController的分类方法然后...

  • runtime无埋点实战

    前言:书写,为了更好地思考。 说来惭愧,无埋点早在一年半之前就已经研究了,但是由于懒的原因一直没有写文章去分析,导...

  • RunTime埋点的思考

    一、什么是埋点?埋点的作用是什么? 二、常规的处理方式是怎样的? 三、我们可以怎样优化? 四、怎样使用RunTim...

  • iOS - Runtime 无埋点实现

    一、创建工具类 NSObject+Swizzling 创建工具类,里面包含以下四个方法,这样可以针对不同的需求进行...

  • 数据埋点之认识埋点

    前言 通过阅读本篇,你将获得以下三方面的知识: 什么是埋点? 埋点的用途? 埋点的分类? 一、什么是埋点 数据埋点...

  • iOS - runtime的一些运用场景

    首先,归纳下Runtime的几个使用场景。 做用户埋点统计 处理异常崩溃(NSDictionary, NSMuta...

  • RunTime实现无侵入全局埋点

    无埋点,不是不需要埋点,更确切地说是“全埋点”,只是埋点代码不会出现在业务代码中优点:容易管理和维护。并且可移植性...

网友评论

      本文标题:2019-04-10 runtime之埋点

      本文链接:https://www.haomeiwen.com/subject/jqugiqtx.html