您的位置:首页 > 其它

到设定的时间让程序退出登录

2016-03-16 18:08 477 查看
@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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: