简体   繁体   English

根据用户活动退出

[英]Log out depending on user activity

I have been looking for this answer from a while but I could not get find solution. 我一直在寻找这个答案,但我无法找到解决方案。 Can any one please tell me how do we calculate time in back ground since user made any interaction with the app. 任何人都可以告诉我,由于用户与应用程序进行任何交互,我们如何计算后台的时间。 In few websites if you don't interact with the web page for a while you will be logged out. 在少数网站中如果您暂时不与网页交互,您将被注销。 I am looking for this functionality. 我正在寻找这个功能。

I would really appreciate your time. 我真的很感激你的时间。 Thanks 谢谢

I think you can catch events via a UIApplication custom subclass. 我认为你可以通过UIApplication自定义子类捕获事件。 You can restart your idle timer there without mucking up all of your code. 您可以在那里重新启动空闲计时器而不会破坏所有代码。 Here's how to do it: 这是怎么做的:

Make your own UIApplication subclass: 创建自己的UIApplication子类:

@interface MyUIApplication : UIApplication {
  NSTimer *idleTimer;
}
@property(nonatomic,retain) NSTimer *idleTimer;

In your main() 在你的主()

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

In your application subclass.m, override sendEvent: and sendAction:. 在您的应用程序subclass.m中,重写sendEvent:和sendAction:。 See Apple docs here : 见苹果文档在这里

- (void)sendEvent:(UIEvent *)event {
  [super sendEvent:event];
  [self. idleTimer invalidate];
  self. idleTimer = [NSTimer scheduledTimerWithTimeInterval:kIDLE_TIME target:self selector:@selector(logout:) userInfo:nil repeats:NO];}

... same for send action. ......发送动作相同。 You can put your logout logic in this class, too: 您也可以将退出逻辑放在此类中:

- (void)logout:(NSTimer *)timer {
  // do logout
}

You should still logout (and invalidate the timer) in your app delegate when the app resigns: 当应用程序辞职时,您仍应在应用程序委托中注销(并使计时器无效):

- (void)applicationWillResignActive:(UIApplication *)application {
  [application logout:nil];
}

You save a UNIX timestamp of the last interaction and then you compare it to a current one. 您保存上次交互的UNIX时间戳,然后将其与当前交互进行比较。 If the difference between them is greater than your time limit, you log user out. 如果它们之间的差异大于您的时间限制,则将用户注销。

我会记录时间- (void)applicationWillEnterForeground:(UIApplication *)application ,以及- (void)applicationWillEnterBackground:(UIApplication *)application ,计算不同,检查是否超过某个值,从上一个会话注销用户。

// Extend method of UIApplication 
- (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:NO];
        }
    }
}

- (void) resetIdleTimer:(BOOL)force {
    // Don't bother resetting timer unless it's been at least 5 seconds since the last reset.
    // But we need to force a reset if the maxIdleTime value has been changed.
    NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
    if (force || (now - lastTimerReset) > 5.0) {
        // DebugLog(@"Reset idle timeout with value %f.", maxIdleTime);
        lastTimerReset = now;
        // Assume any time value less than one second is zero which means disable the timer.
        // Handle values > one second.
        if (maxIdleTime > 1.0) {
            // If no timer yet, create one
            if (idleTimer == nil) {
                // Create a new timer and retain it.
                idleTimer = [[NSTimer scheduledTimerWithTimeInterval:maxIdleTime target:self selector:@selector(idleTimeExceeded) userInfo:nil repeats:NO] retain];
            }
            // Otherwise reset the existing timer's "fire date".
            else {
                [idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:maxIdleTime]];
            }
        }
        // If maxIdleTime is zero (or < 1 second), disable any active timer.
        else {
            if (idleTimer) {
                [idleTimer invalidate];
                [idleTimer release];
                idleTimer = nil;
            }
        }
    }
}

- (void) idleTimeExceeded {
    NSLog(@"Idle time limit of %f seconds exceeded -- ending application", maxIdleTime);
    // Force application to end
    // self.dealloc; -- There's a dealloc in StartProcessViewController.m, but I'm not sure we ought to dealloc the UIApplication object.
    _Exit(0);
}

One way to archive this is setting a cookie with a login hash that has an expire time of X minutes every time the user request a page. 存档的一种方法是使用登录哈希设置cookie,每次用户请求页面时,该哈希的过期时间为X分钟。 If the cookie isn't present the user is logged out. 如果cookie不存在,则用户将被注销。 This could also be a session cookie. 这也可以是会话cookie。 Of course that only works if you're talking about a webapp :) 当然,这只有在你谈论webapp时才有效:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM