您的位置:首页 > 移动开发 > IOS开发

ios开发之 时间日期差计算

2015-08-28 15:56 549 查看
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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: