第十五篇、程序返回前台的时间差(常用于显示广告)
生活随笔
收集整理的这篇文章主要介绍了
第十五篇、程序返回前台的时间差(常用于显示广告)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
如果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];}
转载于:https://www.cnblogs.com/HJQ2016/p/5814771.html
总结
以上是生活随笔为你收集整理的第十五篇、程序返回前台的时间差(常用于显示广告)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 给交叉编译工具建立软连接用脚本
- 下一篇: Cadence 建立封装:多个引脚于芯片