一.统计信息
1.目前做的Hook埋点位置,待补
- 页面的停留时间(viewWillAppear+viewWillDisappear)
- 按钮点击事件(setTarget:action:)
- 列表点击事件(didSelectRowAtIndexPath:)
2.先写HookTool
#pragma mark - Hook分类方法
+(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);
}
}
#pragma mark - Hook代理方法
+ (void)swizzlingInClass:(Class)cls swizClass:(Class)swcls originalSelector:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector;
{
Method originalMethod = class_getInstanceMethod(cls, originalSelector);
Method replacedMethod = class_getInstanceMethod(swcls, swizzledSelector);
IMP replacedMethodIMP = method_getImplementation(replacedMethod);
// 将样替换的方法往代理类中添加, (一般都能添加成功, 因为代理类中不会有我们自定义的函数)
BOOL didAddMethod =
class_addMethod(cls,
swizzledSelector,
replacedMethodIMP,
method_getTypeEncoding(replacedMethod));
if (didAddMethod) {// 添加成功
// 获取新方法在代理类中的地址
Method newMethod = class_getInstanceMethod(cls, swizzledSelector);
// 交互原方法和自定义方法
method_exchangeImplementations(originalMethod, newMethod);
}else{// 如果失败, 则证明自定义方法在代理方法中, 直接交互就可以
method_exchangeImplementations(originalMethod, replacedMethod);
}
}
3.页面停留时间
@implementation UIViewController (UserStatics)
+(void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL originalSelector_viewappear = @selector(viewWillAppear:);
SEL swizzledSelector_viewappear = @selector(swiz_viewWillAppear:);
[JSXHookTool swizzlingInClass:[self class] originalSelector:originalSelector_viewappear swizzledSelector:swizzledSelector_viewappear];
SEL originalSelector_viewdisappear = @selector(viewWillDisappear:);
SEL swizzledSelector_viewdisappear = @selector(swiz_viewWillDisappear:);
[JSXHookTool swizzlingInClass:[self class] originalSelector:originalSelector_viewdisappear swizzledSelector:swizzledSelector_viewdisappear];
});
}
#pragma mark - Method Swizzling
- (void)swiz_viewWillAppear:(BOOL)animated
{
//SDLog(@"页面展示:%@",NSStringFromClass(self.class));
//友盟统计
[MobClick beginLogPageView:NSStringFromClass(self.class)];
[self swiz_viewWillAppear:animated];
}
- (void)swiz_viewWillDisappear:(BOOL)animated
{
//SDLog(@"页面消失:%@",NSStringFromClass(self.class));
//友盟统计
[MobClick endLogPageView:NSStringFromClass(self.class)];
[self swiz_viewWillDisappear:animated];
}
4.按钮的点击事件
@implementation UIControl (UserStatics)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL originalSelector = @selector(sendAction:to:forEvent:);
SEL swizzledSelector = @selector(swiz_sendAction:to:forEvent:);
[JSXHookTool swizzlingInClass:[self class] originalSelector:originalSelector swizzledSelector:swizzledSelector];
});
}
#pragma mark - Method Swizzling
- (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;
{
//统计设置点击次数
[MobClick event:[NSString stringWithFormat:@"%@-%@",target,NSStringFromSelector(action)]];
//SDLog(@"按钮点击%@",[NSString stringWithFormat:@"%@-%@",target,NSStringFromSelector(action)]);
}
5.UITableViewCell点击事件
@implementation UITableView (UserStatics)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL originalSelector = @selector(setDelegate:);
SEL swizzledSelector = @selector(swiz_setDelegate:);
[JSXHookTool swizzlingInClass:[self class] originalSelector:originalSelector swizzledSelector:swizzledSelector];
});
}
-(void)swiz_setDelegate:(id<UITableViewDelegate>)delegate
{
[self swiz_setDelegate:delegate];
Class class = [delegate class];
SEL originalSelector = @selector(tableView:didSelectRowAtIndexPath:);
SEL swizzledSelector = @selector(swiz_tableView:didSelectRowAtIndexPath:);
[JSXHookTool swizzlingInClass:class originalSelector:originalSelector swizzledSelector:swizzledSelector];
[JSXHookTool swizzlingInClass:class swizClass:self.class originalSelector:originalSelector swizzledSelector:swizzledSelector];
}
#pragma mark - Method Swizzling
-(void)swiz_tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//插入埋点代码
//SDLog(@"所属控制器%@",NSStringFromClass(tableView.viewController.class));
[self swiz_tableView:tableView didSelectRowAtIndexPath:indexPath];
}
网友评论