iosobjective-ciphoneidle-timer

iPhone: Detecting user inactivity/idle time since last screen touch


Has anybody implemented a feature where if the user has not touched the screen for a certain time period, you take a certain action? I'm trying to figure out the best way to do that.

There's this somewhat-related method in UIApplication:

[UIApplication sharedApplication].idleTimerDisabled;

It'd be nice if you instead had something like this:

NSTimeInterval timeElapsed = [UIApplication sharedApplication].idleTimeElapsed;

Then I could set up a timer and periodically check this value, and take some action when it exceeds a threshold.

Hopefully that explains what I'm looking for. Has anyone tackled this issue already, or have any thoughts on how you would do it? Thanks.


Solution

  • Here's the answer I had been looking for:

    Have your application delegate subclass UIApplication. In the implementation file, override the sendEvent: method like so:

    - (void)sendEvent:(UIEvent *)event {
        [super sendEvent:event];
    
        // Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
        NSSet *allTouches = [event allTouches];
        if ([allTouches count] > 0) {
            // allTouches count only ever seems to be 1, so anyObject works here.
            UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
            if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
                [self resetIdleTimer];
        }
    }
    
    - (void)resetIdleTimer {
        if (idleTimer) {
            [idleTimer invalidate];
            [idleTimer release];
        }
    
        idleTimer = [[NSTimer scheduledTimerWithTimeInterval:maxIdleTime target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO] retain];
    }
    
    - (void)idleTimerExceeded {
        NSLog(@"idle time exceeded");
    }
    

    where maxIdleTime and idleTimer are instance variables.

    In order for this to work, you also need to modify your main.m to tell UIApplicationMain to use your delegate class (in this example, AppDelegate) as the principal class:

    int retVal = UIApplicationMain(argc, argv, @"AppDelegate", @"AppDelegate");