Make Better Things



I like to make better things.

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.

Category: iPhone

Tagged: ,

7 Responses

  1. Raj says:

    Hi,
    Thanks for the post. This is really helpful for me. It is working fine. But in my application there is ModalViewController. When ever I am calling ModalViewController view (named ActivityViewController), I am getting SIGABRT error in UIApplicationMain function at the third argument( i.e, at “IdleTimeCheck” ) in main.m
    error: Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key closeIcon.

    Can you please help me how to solve this bug?

  2. Rushikesh says:

    Hi,

    Thank for the post. It really helped me. I have one doubt. Can you please help me with one thing. Im not able to understand where to declare maxIdleTime and idleTimer variable. And what type of variable it’ll be?
    I want to check whether my application is idle for 30 secs.

    Thank you for your help.

    • Saurabh says:

      “maxIdleTime” is the amount of time which you want to allow for your application. This program will detect this time and will give you event if app is idle for this time amount. “idleTimer” is just an instance of NSTimer you can declare it in your IdleTimeCheck.h file

  3. joseph says:

    what is “idleTimer” there?
    how can i declare it properly?

    • Saurabh says:

      “maxIdleTime” is the amount of time which you want to allow for your application. This program will detect this time and will give you event if app is idle for this time amount. “idleTimer” is just an instance of NSTimer you can declare it in your IdleTimeCheck.h file

Leave a Reply