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];
            }
  • 相关阅读:
    219. Contains Duplicate II
    189. Rotate Array
    169. Majority Element
    122. Best Time to Buy and Sell Stock II
    121. Best Time to Buy and Sell Stock
    119. Pascal's Triangle II
    118. Pascal's Triangle
    88. Merge Sorted Array
    53. Maximum Subarray
    CodeForces 359D Pair of Numbers (暴力)
  • 原文地址:https://www.cnblogs.com/HJQ2016/p/5814771.html
Copyright © 2011-2022 走看看