第十五篇、程序返回前台的时间差(常用于显示广告)

 

如果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];
        }

 

posted on 2016-08-28 12:56  久冬不雨  阅读(222)  评论(0编辑  收藏  举报