登录超时自动退出,计算时间差-b
2016-09-27 23:24
225 查看
// 此方法适用于所有被创建过的controller,且当前controller生命周期存在,如有错误的地方望大神斧正
// 说一下我们的需求和实现原理,需求:在点击home键退出但没有滑飞它,5分钟之后需要重新登录(其实和平安一账通的登录很像) ;实现原理:添加观察者,不用我们手动发送通知,系统会自动发送通知,计算时间差,然后就可以啦😆
// 贴代码图片
屏幕快照 2016-09-22 下午2.54.31.png
// 我是控制的300s,就是5分钟,超过就弹个框,点击后退出
屏幕快照 2016-09-22 下午3.23.01.png
// 如果监听通知写在viewDidLoad里面,就不用再移除啦
// 说一下我们的需求和实现原理,需求:在点击home键退出但没有滑飞它,5分钟之后需要重新登录(其实和平安一账通的登录很像) ;实现原理:添加观察者,不用我们手动发送通知,系统会自动发送通知,计算时间差,然后就可以啦😆
// 贴代码图片
屏幕快照 2016-09-22 下午2.54.31.png
// 我是控制的300s,就是5分钟,超过就弹个框,点击后退出
屏幕快照 2016-09-22 下午3.23.01.png
// 贴代码 -(void)viewWillAppear:(BOOL)animated { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackgroundNotification) name:UIApplicationDidEnterBackgroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForegroundNotification) name:UIApplicationWillEnterForegroundNotification object:nil]; } -(void)appDidEnterBackgroundNotification{ NSDate date = [NSDate date]; NSUserDefaults user = [NSUserDefaults standardUserDefaults]; [user setObject:date forKey:K_LOGOUTTIME_KEY]; [user synchronize]; NSLog(@"存储时间 =============== ======= %@",date); } -(void)appWillEnterForegroundNotification { NSDate dateLast = [[NSUserDefaults standardUserDefaults] objectForKey:K_LOGOUTTIME_KEY]; 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] > 300) { HPAlertController *alert = [[HPAlertController alloc]initWithViewController:self]; AlertData *data = [AlertData alertDataTarget:self Title:@"提示" message:@"您登录超时了哦~" itemTitleSelectorStrPairs:@"确认",@"ensureLogAgain",nil]; [alert showAlertWithData:data]; } return timeString; } -(void)ensureLogAgain { [self logoutViewController]; }
// 如果监听通知写在viewDidLoad里面,就不用再移除啦
-(void)viewDidDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter]removeObserver:self]; }
相关文章推荐
- Java设置session超时(失效)的时间 在一般系统登录后,都会设置一个当前session失效的时间,以确保在用户长时间不与服务器交互,自动退出登录,销毁session 具体设置的方法有三种:
- ac命令根据/var/log/wtmp文件登录退出时间计算用户连接时间
- windows&linux设置系统自动退出时间(无操作超时设置)
- ac命令根据/var/log/wtmp文件登录退出时间计算用户连接时间
- Linux登录超时自动退出处理办法
- 通过SSH的过滤器及Cookie实现自动登录2周不退出
- SecureCRT自动登录Linux并设置时间
- 更改登录屏幕保护程序超时时间
- 实现“记录用户登录时间和退出时间,同时记录用户IP,并且要记录用户IP对应的省和城市地址”
- 让Windows 7自动显示详细登录时间
- .php会员登录权限和超时退出
- Mac OS X(L1-3): 登录/退出自动运行程序的设置
- windows2003登录超时后,自动注销的解决办法
- ecshop登录 自动退出问题
- 网站自动登录实现---刷在线时间
- 用户登录体验(自动时间锁)
- 使用AutoIT实现自动登录/退出客户端软件,删除日志
- 修改红旗Linux,Windows菜单时间,删除Linux登录密码,设置Linux自动登录
- date 自动计算时间
- 20.2.3.Setting the Login Timeout设置登录超时时间