9w7oba
Last Updated: September 01, 2018
·
7.137K
· novalagung

iOS - Perform action if there is no user interaction in specific duration

First, create constant for handling timeout duration in seconds, and NSTimer instance for idle detection.

// in your SomeViewController.h file
// ...

#define kTimeoutUserInteraction 3

// ...

NSTimer *idleTimer;

In .m, adding these functions

// in your SomeViewController.m file
// ...

# pragma mark timer

- (void)resetIdleTimer {
    if (!idleTimer) {
        idleTimer = [NSTimer scheduledTimerWithTimeInterval:kTimeoutUserInteraction target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO];
        [[NSRunLoop currentRunLoop] addTimer:idleTimer forMode:NSDefaultRunLoopMode];
    } else {
        if (fabs([idleTimer.fireDate timeIntervalSinceNow]) < kTimeoutUserInteraction - 1.) {
            idleTimer.fireDate = [NSDate dateWithTimeIntervalSinceNow:kTimeoutUserInteraction];
        }
    }
}

- (void)idleTimerExceeded {
    idleTimer = nil;
    // do something
    [self resetIdleTimer];
}

- (UIResponder *)nextResponder {
    [self resetIdleTimer];
    return [super nextResponder];
}

// ...

in short, nextResponder is called in every user interaction. In resetIdleTimer, idleTimer is checked wether it's outdate or not, idleTimer going to be renewed if it's outdated. \\ do something is called when timer is exceeded.

1 Response
Add your response

30568

it'd be really nice if there was a more granular way https://itunes.red/ of disabling subreddit styles than just on or off, it'd be awesome to be able to disable subreddit's custom background colors and text colors without disabling their header images or accent colors - i also browse old reddi https://adobereader.onl/ t with css turned off globally, and when i first started using the redesign i really liked the way individual subreddits had a distinct identity without being overbearing. but now that a lot of subreddits are starting to get their overbearing styles implemented again, if i had to make a choice between on or off i'd probably turn them all off again. https://discord.software/

about 1 month ago ·