iOS开发-导航栏统计需求

统计需求

  • 页面展示次数
  • 页面展示时间
  • 页面来源

解决方案

  • 利用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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容