|
|
@@ -19,6 +19,8 @@
|
|
|
//#import "AFHTTPSessionManager.h"
|
|
|
@interface AppDelegate ()
|
|
|
|
|
|
+@property (nonatomic,strong) NSTimer *heartBeat;
|
|
|
+
|
|
|
@end
|
|
|
|
|
|
@implementation AppDelegate
|
|
|
@@ -231,6 +233,8 @@
|
|
|
- (void)applicationDidEnterBackground:(UIApplication *)application {
|
|
|
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
|
|
|
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
|
|
|
+
|
|
|
+ [self stopHeartBeat];
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -241,6 +245,11 @@
|
|
|
|
|
|
- (void)applicationDidBecomeActive:(UIApplication *)application {
|
|
|
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
|
|
|
+
|
|
|
+ if (self.bLogin) {
|
|
|
+ [self startHeartBeat];
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -254,6 +263,13 @@
|
|
|
self.user = user;
|
|
|
self.password = pwd;
|
|
|
self.bLogin = true;
|
|
|
+
|
|
|
+ // 延迟1s启动心跳,以避免在登录框还在Dismiss动画过程中再次调用Dismiss
|
|
|
+ __weak typeof(self) weakself = self;
|
|
|
+ dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
|
|
|
+ dispatch_after(time, dispatch_get_main_queue(), ^{
|
|
|
+ [weakself heartBeatRightNow:YES];
|
|
|
+ });
|
|
|
}
|
|
|
-(void) Logout
|
|
|
{
|
|
|
@@ -269,7 +285,7 @@
|
|
|
self.password=nil;
|
|
|
self.bLogin = false;
|
|
|
|
|
|
-
|
|
|
+ [self stopHeartBeat];
|
|
|
// self.user_icon=nil;
|
|
|
// self.user_type = USER_ROLE_UNKNOWN;
|
|
|
// self.bLogin = false;
|
|
|
@@ -282,4 +298,124 @@
|
|
|
// [self SetMode:nil];
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+#pragma mark - HeartBeat
|
|
|
+
|
|
|
+- (void)markHeartBeatTime {
|
|
|
+
|
|
|
+ NSDate *date = [NSDate date];
|
|
|
+ NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
|
|
|
+ [userDefaults setValue:date forKey:@"heartBeatTime"];
|
|
|
+ [userDefaults synchronize];
|
|
|
+}
|
|
|
+
|
|
|
+- (BOOL)checkHearBeatTimeOut {
|
|
|
+ NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
|
|
|
+ // 前一次成功心跳的时间
|
|
|
+ NSDate *preTime = [userDefaults valueForKey:@"heartBeatTime"];
|
|
|
+
|
|
|
+ if (preTime) {
|
|
|
+ NSDate *curTime = [NSDate date];
|
|
|
+
|
|
|
+ NSTimeInterval timeInterval = [curTime timeIntervalSinceDate:preTime];
|
|
|
+ NSTimeInterval timeOutInterval = 1 * 60 * 60;
|
|
|
+ if (timeInterval > timeOutInterval) {
|
|
|
+ return YES;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return NO;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)heartBeatRightNow:(BOOL)rightNow {
|
|
|
+ if (rightNow) {
|
|
|
+ [self heartBeatAction:nil];
|
|
|
+ }
|
|
|
+
|
|
|
+ NSTimeInterval heartBeatTimeInterval = 3 * 60;
|
|
|
+ dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
|
|
+ self.heartBeat = [NSTimer scheduledTimerWithTimeInterval:heartBeatTimeInterval target:self selector:@selector(heartBeatAction:) userInfo:nil repeats:YES];
|
|
|
+ [[NSRunLoop currentRunLoop] addTimer:self.heartBeat forMode:NSDefaultRunLoopMode];
|
|
|
+ [[NSRunLoop currentRunLoop] run];
|
|
|
+ });
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (void)startHeartBeat {
|
|
|
+
|
|
|
+
|
|
|
+ [self heartBeatRightNow:YES];
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (void)stopHeartBeat {
|
|
|
+ if (self.heartBeat) {
|
|
|
+ [self.heartBeat invalidate];
|
|
|
+ self.heartBeat = nil;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (void)heartBeatTimeout {
|
|
|
+ __weak typeof(self) weakself = self;
|
|
|
+ dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
+
|
|
|
+ UIApplication *application = [UIApplication sharedApplication];
|
|
|
+ UINavigationController *rootVC = (UINavigationController *)application.keyWindow.rootViewController;
|
|
|
+
|
|
|
+ // 超时登出
|
|
|
+ [weakself Logout];
|
|
|
+ // 提示登录
|
|
|
+ UIViewController* presentVC=rootVC.presentedViewController;
|
|
|
+ if (presentVC) { //如果当前已有模态对话框,则关闭当前的模态框
|
|
|
+ [presentVC dismissViewControllerAnimated:NO completion:^{
|
|
|
+ // 退回到初始视图
|
|
|
+ [rootVC popToRootViewControllerAnimated:NO];
|
|
|
+ RootViewController* topvc= (RootViewController*)rootVC.topViewController;
|
|
|
+ [topvc dealWithUILogout];
|
|
|
+ [topvc presentLogin:topvc.ibSignin];
|
|
|
+ }];
|
|
|
+ } else {
|
|
|
+ // 退回到初始视图
|
|
|
+ [rootVC popToRootViewControllerAnimated:NO];
|
|
|
+ RootViewController* topvc= (RootViewController*)rootVC.topViewController;
|
|
|
+ [topvc dealWithUILogout];
|
|
|
+ [topvc presentLogin:topvc.ibSignin];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+- (void)heartBeatAction:(NSTimer *)timer {
|
|
|
+
|
|
|
+ __weak typeof(self) weakself = self;
|
|
|
+ dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
|
|
+
|
|
|
+ int result = [ACNetwork heartBeat];
|
|
|
+// int result = 2;
|
|
|
+
|
|
|
+ if (result == 2) {
|
|
|
+ // 心跳成功才处理计时
|
|
|
+ [self markHeartBeatTime];
|
|
|
+
|
|
|
+ } else if (result == 4) { // 超时
|
|
|
+
|
|
|
+ [weakself heartBeatTimeout];
|
|
|
+
|
|
|
+
|
|
|
+ } else if ([weakself checkHearBeatTimeOut]) {// 本地检查超时(上一次成功心跳距现在的间隔时间超出1h)
|
|
|
+
|
|
|
+ [weakself heartBeatTimeout];
|
|
|
+ }
|
|
|
+
|
|
|
+ DebugLog(@"heart beat result %d",result);
|
|
|
+
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+- (void)dealloc {
|
|
|
+ [self stopHeartBeat];
|
|
|
+}
|
|
|
+
|
|
|
@end
|