登录超时退出到登录界面

  • (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];
    }

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容