iOS 友盟统计页面访问路径(Category)
前言:项目需求添加友盟页面访问路径及访问深度
友盟官方文档中介绍,在每个UIViewController的两个生命周期方法中执行两个统计的两个方法(注意:必须配对调用beginLogPageView:和endLogPageView:两个函数来完成自动统计,若只调用某一个函数不会生成有效数据;在该页面展示时调用beginLogPageView:,当退出该页面时调用endLogPageView。)
在ViewController类的viewWillAppear: 和 viewWillDisappear:中配对调用如下方法:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[MobClick beginLogPageView:@"Pagename"]; //("Pagename"为页面名称,可自定义)
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[MobClick endLogPageView:@"Pagename"];
}
其实集成已经特别很简单,但是现在的问题在每个Controller里面的去调用写这个方法显然是不科学的,尤其现在已经成熟的项目那要加多少😁,那么就用Category一劳永逸的解决完成这个需求!
1.创建ViewController分类
- UIViewController+WTVYXJAS.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIViewController (WTVYXJAS)
/** 友盟统计Log*/
@property (nonatomic ,copy) NSString * umLogAs;
@end
NS_ASSUME_NONNULL_END
- UIViewController+WTVYXJAS.m
#import "UIViewController+WTVYXJAS.h"
@implementation UIViewController (WTVYXJAS)
//参考资料:http://blog.leichunfeng.com/blog/2015/06/14/objective-c-method-swizzling-best-practice/
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL viewWillOriginaSelector = @selector(viewWillAppear:);
SEL viewWillSwizzledSelector = @selector(as_viewWillAppear:);
SEL viewWillDissOriginaSelector = @selector(viewWillDisappear:);
SEL viewWillDissSwizzledSelector = @selector(as_viewWillDisappear:);
Method viewWillAppear = class_getInstanceMethod(class, @selector(viewWillAppear:));
Method as_viewWillAppear = class_getInstanceMethod(class, @selector(as_viewWillAppear:));
BOOL viewWillSuccess = class_addMethod(class, viewWillOriginaSelector, method_getImplementation(as_viewWillAppear), method_getTypeEncoding(as_viewWillAppear));
if (viewWillSuccess) {
class_replaceMethod(class, viewWillSwizzledSelector, method_getImplementation(viewWillAppear), method_getTypeEncoding(viewWillAppear));
}else{
method_exchangeImplementations(viewWillAppear, as_viewWillAppear);
}
Method viewWillDisappear = class_getInstanceMethod(class, @selector(viewWillDisappear:));
Method as_viewWillDisappear = class_getInstanceMethod(class, @selector(as_viewWillDisappear:));
BOOL viewWillDissSuccess = class_addMethod(class, viewWillDissOriginaSelector, method_getImplementation(as_viewWillDisappear), method_getTypeEncoding(as_viewWillDisappear));
if (viewWillDissSuccess) {
class_replaceMethod(class, viewWillDissSwizzledSelector, method_getImplementation(viewWillDisappear), method_getTypeEncoding(viewWillDisappear));
}else{
method_exchangeImplementations(viewWillDisappear, as_viewWillDisappear);
}
});
}
- (void)as_viewWillAppear:(BOOL)animated{
if (self.title.length) {
NSLog(@"开始路径%@->%@ -> %s",NSStringFromClass(self.class),self.title,__func__);
[MobClick beginLogPageView:self.title];
}
[self as_viewWillAppear:animated];
}
- (void)as_viewWillDisappear:(BOOL)animated{
if (self.title.length) {
NSLog(@"结束路径%@->%@ -> %s",NSStringFromClass(self.class),self.title,__func__);
[MobClick endLogPageView:self.title];
}
[self as_viewWillDisappear:animated];
}
- (void)setUmLogAs:(NSString *)umLogAs{
objc_setAssociatedObject(self, @selector(umLogAs), umLogAs, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *)umLogAs{
//_cmd在Objective-C的方法中表示当前方法的selector,正如同self表示当前方法调用的对象实例一样。
return objc_getAssociatedObject(self, _cmd);
}
@end
2.关于自定义Controller或者自定义导航栏需实现
//友盟统计需要
- (void)loadView{
[super loadView];
self.title = @"自定义导航栏";
}
3.如果在VC里面实现这两个方法必须实现[super xxxxx])不然被统计不上去
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated]; //必须实现
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated]; //必须实现
}