-
(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//创建通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackgroundNotification) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForegroundNotification) name:UIApplicationWillEnterForegroundNotification object:nil];
} -
(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
//移除通知
[[NSNotificationCenter defaultCenter]removeObserver:self];
} -
(void)appDidEnterBackgroundNotification{
NSDate *date = [NSDate date];
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setObject:date forKey:@"date"];
[user synchronize];
NSLog(@"存储时间 =============== ======= %@",date);
} -
(void)appWillEnterForegroundNotification {
NSDate *dateLast = [[NSUserDefaults standardUserDefaults] objectForKey:@"date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [dateFormatter stringFromDate:dateLast];
[self intervalSinceNow:strDate];
}
// 计算某一时间到当前时间
-
(NSString *)intervalSinceNow: (NSString *) theDate
{
NSArray *timeArray=[theDate componentsSeparatedByString:@"."];
theDate=[timeArray objectAtIndex:0];
NSDateFormatter *date=[[NSDateFormatter alloc] init];
[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate d=[date dateFromString:theDate];
NSTimeInterval late=[d timeIntervalSince1970]1;
NSDate dat = [NSDate date];
NSTimeInterval now=[dat timeIntervalSince1970]1;
NSString *timeString=@"";
NSTimeInterval cha= fabs(late-now); // 求绝对值
timeString = [NSString stringWithFormat:@"%f", cha];
timeString = [timeString substringToIndex:timeString.length-7];
if ([timeString intValue] > 60) {
[self shouNoticeViewWithNoticeString:@"您登录超时了哦"];
}
return timeString;
} -
(void)ensureLogAgain {
LoginViewController *login = [[LoginViewController alloc] init];
[self.navigationController pushViewController:login animated:YES];
}
网友评论