博客> ios低耦合埋点(UBS)的简单实现
ios低耦合埋点(UBS)的简单实现
2017-12-12 09:06 评论:0 阅读:344 chuchengpeng
ios runtime 埋点 UBS 低耦合

利用runtime和category,简单的实现埋点功能。 直接上代码: 利用runtime交换UIViewController原生方法实现:

  • (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ SEL orginS = @selector(viewWillAppear:); SEL replaceS = @selector(ccp_viewWillAppear:); [CCPRuntime exchangeInClass:[self class] orginS:orginS placeS:replaceS]; SEL orginS1 = @selector(viewWillDisappear:); SEL replaceS1 = @selector(ccp_viewWillDisappear:); [CCPRuntime exchangeInClass:[self class] orginS:orginS1 placeS:replaceS1]; }); } 方法重构:
  • (void)ccp_viewWillAppear:(BOOL)animated { self.title = NSStringFromClass([self class]); [self enterViewWithPageID:@"123"]; [self ccp_viewWillAppear:animated]; }

  • (void)ccp_viewWillDisappear:(BOOL)animated { [self leaveViewWithPageID:@"123"]; [self ccp_viewWillDisappear:animated]; }

可以实现页面次数统计和页面停留时间统计.

交换UIControl的原生方法实现:

  • (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ SEL originS = @selector(sendAction:to:forEvent:); SEL replaceS = @selector(ccp_sendAction:to:forEvent:); [CCPRuntime exchangeInClass:[self class] orginS:originS placeS:replaceS]; }); }

重构:

  • (void)ccp_sendAction:(SEL)action to:(id)target forEvent:(UIEvent )event { if ([[[event allTouches] anyObject] phase] == UITouchPhaseBegan) { NSString actionString = NSStringFromSelector(action); NSString *targetName = NSStringFromClass([target class]); NSLog(@"controlTouch---%@---%@",actionString,targetName); } [self ccp_sendAction:action to:target forEvent:event]; }

可以实现按钮点击次数统计 demo地址:https://github.com/coolboy-ccp/CCPUBS

收藏
1
sina weixin mail 回到顶部