@interface MBWindow : UIWindow
@end
@implementation MBWindow
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
/**
* 程序超时控制。在"退出时间设定"(服务设定/退出时间设定)设置后,用户登录后,超过该时间屏幕没有响应,强行退出登录。
*/
- (void)sendEvent:(UIEvent *)event{
[super sendEvent:event];
NSInteger timeoutInterval = [[NSUserDefaults standardUserDefaults] integerForKey:@"time"];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(appTimeout) object:nil];
if ([USER_DEFAULT boolForKey:kIsLogin]&&timeoutInterval) {
[self performSelector:@selector(appTimeout) withObject:nil afterDelay:timeoutInterval * 60];
}
}
- (void)userLogout{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(appTimeout) object:nil];
}
- (void)appTimeout{
NSInteger timeInterval = [[NSUserDefaults standardUserDefaults] integerForKey:@"time"];
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"程序%ld分钟没操作,请重先登录",(long)timeInterval] delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alert show];
//到这里其实就是强制让其 退出登录 系统的时间到了之后
[USER_DEFAULT setBool:NO forKey:kIsLogin];
// [USER_DEFAULT setBool:NO forKey:kAutoLogin];
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLoginSuccess object:nil];
});
}
self.window = [[MBWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
获取登录 的状态
//判断是否登录
BOOL isLogin = [USER_DEFAULT boolForKey:kIsLogin];
BOOL autoLogin = [USER_DEFAULT boolForKey:kAutoLogin];
if (isLogin && autoLogin) {
_isLogin = YES;
}else{
_isLogin = NO;
}
登录成功把信息和时间存在本地
[[NSUserDefaults standardUserDefaults] setInteger:1000 forKey:@"time"];
[[NSUserDefaults standardUserDefaults] synchronize];