Nov 2, 2011 2
Detecting user inactivity/idle time since last touch on screen
Recently I required to find inactivity of screen in one of my iPad project. Here is the steps by which I completed this task.
Step 1 - Add a class (IdleTimeCheck) in your project which 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.
Step 2 – Modify your UIApplicationMain function in main.m file to use your UIApplication subclass class as principal class.
int retVal = UIApplicationMain(argc, argv, @"IdleTimeCheck",nil);
And its done.
Hope it help someone who is looking for something like this.