美文网首页
iOS开发-导航栏统计需求

iOS开发-导航栏统计需求

作者: Huangbaoqin | 来源:发表于2017-04-18 13:14 被阅读8次

    统计需求

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

    解决方案

    • 利用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
    

    相关文章

      网友评论

          本文标题:iOS开发-导航栏统计需求

          本文链接:https://www.haomeiwen.com/subject/lqmfzttx.html