设定时间让应用从后台回来是否重新启动应用

设定时间让应用从后台回来是否重新启动应用

- (void)applicationDidEnterBackground:(UIApplication *)application

{

    //保存进入后台的时间戳

    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];

    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];

    NSString *lastLaunchTimeString = [formatter stringFromDate:[NSDate date]];

 

    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];

    [def setObject:lastLaunchTimeString forKey:LAST_LAUNCH_TIME];

    [def synchronize];

}

- (void)applicationWillEnterForeground:(UIApplication *)application

{

    //对比进入后台的时间戳大于1小时则重新加载应用

    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];

    NSString *lastLaunchTimeString = [def stringForKey:LAST_LAUNCH_TIME];

 

    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];

    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];

    NSDate *lastTime = [formatter dateFromString:lastLaunchTimeString];

   

    NSString *nowTimeString = [formatter stringFromDate:[NSDate date]];

    NSDate *nowTime = [formatter dateFromString:nowTimeString];

    NSTimeInterval interval = [nowTime timeIntervalSinceDate:lastTime];  

    if (interval > 3600) {

        self.mainCtrl = nil;

        self.mainCtrl.delegate = nil;

        self.window.rootViewController = nil;

        self.window = nil;

        [self application:nil didFinishLaunchingWithOptions:nil];

    }

}

 

你可能感兴趣的:(时间)