统计需求
解决方案
- 利用ViewController的AppealAppear/Disappear方法统计次数和时间
- 重载UINavigationController的Push/Pop方法标记来源
缺点
- 改动太多,所有的ViewController都要改
- 强制使用重载的UINavigationController
使用UINavigationControllerDelegate实现
#import "NavStatistic.h"
@interface NavStatistic ()
@property (nonatomic, assign) NSInteger currentCount;
@property (nonatomic, weak) UIViewController *currentPage;
@property (nonatomic, assign) NSTimeInterval currentShowTime;
@end
@implementation NavStatistic
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
BOOL isPush = NO;
if (navigationController.viewControllers.count > self.currentCount) {
isPush = YES;
}
if (isPush) {
if (self.currentPage) {
NSLog(@"首次展示页面:%@ 来自 %@", NSStringFromClass([viewController class]), NSStringFromClass([self.currentPage class]));
} else {
NSLog(@"首次展示页面:%@", NSStringFromClass([viewController class]));
}
}
if (self.currentPage) {
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
NSTimeInterval duration = currentTime - self.currentShowTime;
NSLog(@"页面 %@ 展示时长 %f", NSStringFromClass([self.currentPage class]), duration);
}
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
self.currentCount = [navigationController.viewControllers count];
self.currentPage = viewController;
self.currentShowTime = [[NSDate date] timeIntervalSince1970];
}
@end