/**
* 每天第一次启动app去签到
*/
-(BOOL)goToSign{
NSDate *now = [NSDate date];
//当前时间的时间戳
NSTimeInterval nowStamp = [now timeIntervalSince1970];
//当天零点的时间戳
NSTimeInterval zeroStamp = [[[NSUserDefaults standardUserDefaults] objectForKey:@"zeroStamp"] doubleValue];
//一天的时间戳
NSTimeInterval oneDay = 60* 60 * 24;
/**
"hasSign"代表了是否当天是否签过到,NO代表没有签过,YES代表已经签过
*/
if(nowStamp - zeroStamp > oneDay){
zeroStamp = [self getTodayZeroStampWithDate:now];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithDouble:zeroStamp] forKey:@"zeroStamp"];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@"hasSign"];
return YES;
}else{
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"hasSign"]) {
return NO;
} else {
return YES;
}
}
}
/**
* 获取当天零点时间戳
*/
- (double)getTodayZeroStampWithDate:(NSDate *)date{
NSDateFormatter *dateFomater = [[NSDateFormatter alloc]init];
dateFomater.dateFormat = @"yyyy年MM月dd日";
NSString *original = [dateFomater stringFromDate:date];
NSDate *ZeroDate = [dateFomater dateFromString:original];
// 今天零点的时间戳
NSTimeInterval zeroStamp = [ZeroDate timeIntervalSince1970];
return zeroStamp;
}
网友评论