如果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]; }
时间: 2024-10-16 04:11:16