登錄超時退出到登錄界面

  • (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //創(chuàng)建通知
    [[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];
    }

// 計算某一時間到當(dāng)前時間

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容