zoukankan      html  css  js  c++  java
  • 第十五篇、程序返回前台的时间差(常用于显示广告)

    如果app在后台待机太久,再次进来前台的时候也应该展示广告,所以在applicationDidEnterBackground的时候应该把时间存起来:

     //程序切入后台,这里要注意GMT时间
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
        [formatter setTimeZone:sourceTimeZone];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        _lastTimeEnterBackGroundStr = [formatter stringFromDate:[NSDate date]];//当前时间

    在applicationWillEnterForeground的时候对比时间差,判断是否显示:

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
            [formatter setTimeZone:sourceTimeZone];
            [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSDate * lastDate = [formatter dateFromString:_lastTimeEnterBackGroundStr];
            NSDate * now = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];
            NSTimeInterval IntervalTime = [now timeIntervalSince1970]*1 - [lastDate timeIntervalSince1970]*1;
            if (IntervalTime>(2*60*60)) {
                [_mainController loadAdvertisedView];
            }
  • 相关阅读:
    冲刺一(5)
    冲刺一(4)
    冲刺一(3)
    构建之法阅读笔记之二
    冲刺一(2)
    冲刺一(1)
    第9周总结
    热词顶会分析
    第8周总结
    构建之法阅读笔记之一
  • 原文地址:https://www.cnblogs.com/HJQ2016/p/5814771.html
Copyright © 2011-2022 走看看