iOS開發之時間日期比較

APP會彈出評分窗口,又或者彈出更新版本窗口,頻率幾乎都會是固定的,這里主要是用了APP在上次打開日期和當前打開的時間差,進而觸發事件.上代碼.

Paste_Image.png
#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];

}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容