0

I want to add logout functionality in my app, so that user gets logout on inactivity/idle time since last screen touch.

Please suggest me the way to implement this functionality.

Some people are telling add this in your code;

- (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");

    }

But my question is where to add this code.

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
  • In first View controller(intialview). – phani Aug 03 '17 at 04:49
  • some one told to me that add this in uiApplication class. I am in confusion –  Aug 03 '17 at 04:51
  • You can create one function to set timer on each screen. Set timer to 0 on each screen (or touch on event). When user goes idle and comes back in the app check the timer value if it exceeds your specified time (for ex., 10mins), you can log out that user. – Rohan Aug 03 '17 at 04:56
  • This link will help you. https://stackoverflow.com/questions/273450/iphone-detecting-user-inactivity-idle-time-since-last-screen-touch – Harshal Shah Aug 04 '17 at 12:41

2 Answers2

2

Get rid of all of your timer code and simply do this. When the user taps, schedule your idle method to be called after X seconds (using performSelector: afterDelay:). Anytime they tap, cancel all scheduled requests (using cancelPreviousPerformRequestsWithTarget:) and request a new one for X seconds.

int secondsUntilTimeout = 120;//time you want until they time-out.

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(idleTimerExceeded) object:nil];//cancel all previously scheduled time-out requests

[self performSelector:@selector(idleTimerExceeded) withObject:nil afterDelay:secondsUntilTimeout];//schedule a new time-out request

So your final code will look like this:

- (void)sendEvent:(UIEvent *)event {
    [super sendEvent:event];
    NSSet *allTouches = [event allTouches];
    if ([allTouches count] > 0) {
        UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
        if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded) {

            int secondsUntilTimeout = 120;//time you want until they time-out.

            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(idleTimerExceeded) object:nil];//cancel all previously scheduled time-out requests

            [self performSelector:@selector(idleTimerExceeded) withObject:nil afterDelay:secondsUntilTimeout];//schedule a new time-out request

        }
    }
}


- (void)idleTimerExceeded {
    NSLog(@"idle time exceeded");
}
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
0

Here is the swift version of code snippet which detects idle time on screen and can fire events such as auto logout/API call/other event call. Go to following simple steps

  1. Initialise following three lines of code to setup duration and schedule time
var secondsUntilTimeout : Int = 60 //Duration(in seconds) after you want to fire and event

NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(callPrintEventAfterTimeExceeded), object: nil) //Cancel if any previous time out request is running

perform(#selector(callPrintEventAfterTimeExceeded), with: nil, afterDelay: TimeInterval(secondsUntilTimeout)) //Set a new time out request to fire an event
  1. Write desired code of any event in following method. You can write your own method name according to the requirement
@objc func callPrintEventAfterTimeExceeded() {
    print("idle time exceeded")
}