iOS method swizzling 面向切面无痕打点

  • 重载类的“+(void)load”方法,在程序加载到内存时利用Runtime的
method_exchangeImplementations

等接口将方法的实现互相交换。当方法被调用时就会被勾住(Hook),执行我们的方法。

@interface HookUtility : NSObject

+ (void)swizzlingInClass:(Class)cls originalSelector:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector;

@end

@implementation HookUtility

+ (void)swizzlingInClass:(Class)cls originalSelector:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector {
    Class class = cls;
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    if (didAddMethod) {
        class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

@end
  • 页面进入的埋点统计,交换ViewController的DidAppear方法的实现以ViewController的类名为识别符进行埋点统计。
@implementation UIViewController (userStastistics)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL originalSelector = @selector(viewWillAppear:);
        SEL swizzledSelector = @selector(swiz_viewWillAppear:);
        [HookUtility swizzlingInClass:[self class] originalSelector:originalSelector swizzledSelector:swizzledSelector];
    });
}

- (void)swiz_viewWillAppear:(BOOL)animated {
    // 埋点上报
    NSString *targetName = NSStringFromClass([self class]);
    NSLog(@"上报到数据统计中心的targetName=%@", targetName);
    // 执行被交换的实现
    [self swiz_viewWillAppear:animated];
}

@end
  • 控件交互事件(点击、拖动、双击...)的埋点统计,交换
sendAction:to:forEvent:

方法实现,以按钮的“类名+target名+action名”作为识别符上报数据统计中心

@implementation UIControl (userStastistics)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL originalSelector = @selector(sendAction:to:forEvent:);
        SEL swizzledSelector = @selector(swiz_sendAction:to:forEvent:);
        [HookUtility swizzlingInClass:[self class] originalSelector:originalSelector swizzledSelector:swizzledSelector];
    });
}

- (void)swiz_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    //插入埋点代码
    [self performUserStastisticsAction:action to:target forEvent:event];
    // 执行被交换的实现
    [self swiz_sendAction:action to:target forEvent:event];
}

- (void)performUserStastisticsAction:(SEL)action to:(id)target forEvent:(UIEvent *)event; {
    
    NSString *className = NSStringFromClass([self class]);
    NSString *targetName = NSStringFromClass([target class]);
    NSString *actionString = NSStringFromSelector(action);
    
    NSLog(@"上报到数据统计中心=%@_%@_%@", className, targetName, actionString);
}

@end
  • 扩展-页面停留时长统计:可通过以上ViewController的扩展类去扩展属性,在DidAppear方法里给该属性赋值,在DidDisappear方法里计算时间差并且交换方法实现达到页面停留时长埋点需求
  • 扩展-批量上报/实时上报:通过本地plist配置文件去自定义一些特殊行为。

你可能感兴趣的:(iOS method swizzling 面向切面无痕打点)