Timer when leaving app and returning on ios

大兔子大兔子 提交于 2019-12-11 11:28:11

问题


Currently on my iOS app, when the user exits to the home screen and goes back into the app, it requests a login credentials which is set in my AppDelegate. But what I am trying to do is if the user goes out of the app and back in within for example 2 minutes, the timer resets and the user does not need to input his password. When the user goes back into the app after 2 minutes, it will alert him to input the password again. Any help will be greatly appreciated. Thanks!


回答1:


Use NSUserDefaults to store the NSDate in your app delegate

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"myDateKey"];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSDate *bgDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey: @"myDateKey"];
    if(fabs([bgDate timeIntervalSinceNow]) > 120.00) {
        //logout
    }
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"myDateKey"];
}

Update:

Good point by @mun chun if the app has to implement something to handle clock changes we can use something like this

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[NSUserDefaults standardUserDefaults] setFloat: [[NSProcessInfo processInfo] systemUptime] forKey:@"myDateKey"];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    float bgTime = [[NSUserDefaults standardUserDefaults] floatForKey: @"myDateKey"];
    if(fabs([[NSProcessInfo processInfo] systemUptime] - bgTime)  > 120.00) {
        //logout
    }
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"myDateKey"];
}

Obviously once the phone restarts the time will be reset in that case we have to make sure we add validation. Also to note is that the myDateKey should be removed in appropriate application modes.




回答2:


User may adjust the system time to earlier when the app is in background. It maybe not reliable to compare a stored time with current system time when app reopened.

We can use NSTimer+BackgroundTask to assure the amount of time elapsed.

In the applicationWillResignActive: delegate, setup a background task and a NSTimer.

When the timer fired (i.e. at 120 seconds), set the session expired, and end the background task.

When the app re-opened, in the applicationDidBecomeActive: delegate, check the session for request login if it expired.

static BOOL sessionActive;
static NSTimer *timer;
static UIBackgroundTaskIdentifier bgTask;

- (void)applicationWillResignActive:(UIApplication *)application
{    
    sessionActive = YES;
    timer  = [NSTimer scheduledTimerWithTimeInterval:120.0 target:self selector:@selector(sessionExpired) userInfo:nil repeats:NO];
    bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{[self sessionExpired];}];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [self cleanup];
    if(!sessionActive)
    {
        //session expired, request login credentials
    }
}

-(void)sessionExpired
{
    [self cleanup];
    sessionActive = NO;
}

-(void)cleanup
{
    if([timer isValid]) [timer invalidate];
    timer = nil;
    [[UIApplication sharedApplication] endBackgroundTask:bgTask];
}


来源:https://stackoverflow.com/questions/27045405/timer-when-leaving-app-and-returning-on-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!