ios开发之 时间日期差计算

来源:互联网 发布:get软件是真鞋么 编辑:程序博客网 时间:2024/05/20 23:36
APP会弹出评分窗口,又或者弹出更新版本窗口,频率几乎都会是固定的,这里主要是用了APP在上次打开日期和当前打开的时间差,进而触发事件.

这里写图片描述

#pragma mark - 版本更新提示频率事件-(void)getCurrentTime{//输出的时间是格林威治标准时间本初子午线穿过哪里NSDate*currentDate = [NSDatedate];NSLog(@"currentDate%@",currentDate);//读取上次打开时间NSDate*userLastOpenDate =[[NSUserDefaultsstandardUserDefaults]objectForKey:@"AppTimeLastOpenDate"];NSLog(@"userLastOpenDate%@",userLastOpenDate);//日历NSCalendar*calendar = [NSCalendarcurrentCalendar];//计算两个日期的差值NSDateComponents*cmps= [calendarcomponents:NSCalendarUnitDayfromDate:userLastOpenDatetoDate:currentDateoptions:NSCalendarMatchStrictly];//定义isAPPUpdateTime全局变量,至于如何定义,请参阅以往文章AppDelegate*appdelegate = (AppDelegate*)[UIApplicationsharedApplication].delegate;NSLog(@"(long)cmps.second%ld",(long)cmps.second);if(cmps.date>0) {appdelegate.isAPPUpdateTime=YES;//版本更新[selfAppVersionCheckRemind];NSLog(@"时间分钟差值--->%@",cmps);}else{appdelegate.isAPPUpdateTime=NO;}NSLog(@"---->%hhd",appdelegate.isAPPUpdateTime);//把当前打开的时间保存起来(如果设置数据之后没有同步,会在将来某一时间点自动将数据保存到Preferences文件夹下面)[[NSUserDefaultsstandardUserDefaults]setObject:currentDateforKey:@"AppTimeLastOpenDate"];//强制让数据立刻保存[[NSUserDefaultsstandardUserDefaults]synchronize];}
0 0
原创粉丝点击