给navigationControler 添加一个分类
@implementation UINavigationController (Track)
- (void)track_pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count < 2) {
return;
}
UIViewController *currentVC = self.viewControllers[self.viewControllers.count - 2];
if (!currentVC || !viewController) {
#if DEBUG
NSAssert(NO, @"currentVC or pushVC not exsit");
#endif
return;
}
if (![currentVC conformsToProtocol:@protocol(VVSnowplowTrackParams)] ||
![viewController conformsToProtocol:@protocol(VVSnowplowTrackParams)]) {
NSLog(@"not conform VVSnowplowTrackParams protocol. Current: %@, Push: %@.", currentVC, viewController);
return;
}
[(id <VVSnowplowTrackParams>)viewController setSpReferrer:[(id <VVSnowplowTrackParams>)currentVC spUri]];
}
交换push 方法
+ (void)vv_swizzleMethod
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [UINavigationController class];
SEL originSelector = @selector(pushViewController:animated:);
SEL swizzledSelector = @selector(new_pushViewController:animated:);
[NSObject exchangeInstanceMethod:class originalSel:originSelector swizzledSel:swizzledSelector];
});
}
- (void)new_pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[self new_pushViewController:viewController animated:animated];
[self track_pushViewController:viewController animated:animated];
}
|