我一直在寻找这个答案,但我无法找到解决方案。任何人都可以告诉我,由于用户与应用程序进行任何交互,我们如何计算后台的时间。在少数网站中如果您暂时不与网页交互,您将被注销。我正在寻找这个功能。
我非常感谢你的时间。 谢谢
答案 0 :(得分:6)
我认为您可以通过UIApplication自定义子类捕获事件。您可以在那里重新启动空闲计时器而不会破坏所有代码。这是如何做到的:
制作您自己的UIApplication子类:
@interface MyUIApplication : UIApplication {
NSTimer *idleTimer;
}
@property(nonatomic,retain) NSTimer *idleTimer;
在你的主要()
int retVal = UIApplicationMain(argc, argv, @"MyUIApplication", nil);
在您的应用程序subclass.m中,重写sendEvent:和sendAction:。请参阅Apple文档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];}
......发送动作相同。您也可以将退出逻辑放在此类中:
- (void)logout:(NSTimer *)timer {
// do logout
}
当应用程序辞职时,您仍应在应用委托中注销(并使计时器无效):
- (void)applicationWillResignActive:(UIApplication *)application {
[application logout:nil];
}
答案 1 :(得分:1)
您保存上次交互的UNIX时间戳,然后将其与当前交互进行比较。如果它们之间的差异大于您的时间限制,则将用户注销。
答案 2 :(得分:1)
我会记录- (void)applicationWillEnterForeground:(UIApplication *)application
的时间,当- (void)applicationWillEnterBackground:(UIApplication *)application
计算不同的时间时,检查它是否超过某个值,从上一个会话中注销用户。
答案 3 :(得分:1)
// 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);
}
答案 4 :(得分:0)
存档的一种方法是设置一个带有登录哈希的cookie,每次用户请求页面时,该哈希的过期时间为X分钟。如果cookie不存在,则用户将被注销。这也可以是会话cookie。当然,这只有在你谈论webapp时才有效:)