根据用户活动注销

发布于 2024-11-27 08:56:47 字数 134 浏览 1 评论 0原文

我一直在寻找这个答案一段时间了,但我找不到解决方案。任何人都可以告诉我,自从用户与应用程序进行任何交互以来,我们如何计算后台的时间。在一些网站中,如果您一段时间不与网页交互,您将被注销。我正在寻找这个功能。

我非常感谢您的宝贵时间。 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一腔孤↑勇 2024-12-04 08:56:47

我认为您可以通过 UIApplication 自定义子类捕获事件。您可以在那里重新启动空闲计时器,而不会弄乱所有代码。操作方法如下:

创建您自己的 UIApplication 子类:

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

在您的 main() 中

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

在您的应用程序 subclass.m 中,覆盖 sendEvent: 和 sendAction:。请参阅此处的 Apple 文档:

- (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];}

。 .. 与发送操作相同。您也可以将注销逻辑放入此类中:

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

当应用程序退出时,您仍然应该在应用程序委托中注销(并使计时器无效):

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

I think you can catch events via a UIApplication custom subclass. 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:

@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:. 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];
}
十秒萌定你 2024-12-04 08:56:47

您保存最后一次交互的 UNIX 时间戳,然后将其与当前的时间戳进行比较。如果它们之间的差异大于您的时间限制,您将注销用户。

You save a UNIX timestamp of the last interaction and then you compare it to a current one. If the difference between them is greater than your time limit, you log user out.

楠木可依 2024-12-04 08:56:47

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

I would log the time when - (void)applicationWillEnterForeground:(UIApplication *)application, and when - (void)applicationWillEnterBackground:(UIApplication *)application, calculate the different, check if it exceed certain value, log off user from previous session.

梅窗月明清似水 2024-12-04 08:56:47
// 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);
}
// 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);
}
风吹雨成花 2024-12-04 08:56:47

归档此问题的一种方法是使用登录哈希值设置 cookie,每次用户请求页面时,该 cookie 的过期时间为 X 分钟。如果 cookie 不存在,则用户将被注销。这也可能是会话 cookie。当然,这只适用于网络应用程序:)

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. If the cookie isn't present the user is logged out. This could also be a session cookie. Of course that only works if you're talking about a webapp :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文