Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 根据用户活动注销_Iphone_Ios - Fatal编程技术网

Iphone 根据用户活动注销

Iphone 根据用户活动注销,iphone,ios,Iphone,Ios,我一直在寻找这个答案,但我找不到解决办法。有谁能告诉我,自从用户与应用程序进行交互以来,我们如何计算后台的时间。在一些网站上,如果你有一段时间不与网页交互,你将被注销。我正在寻找这个功能 如果你能抽出时间,我将不胜感激。 谢谢您保存了上次交互的UNIX时间戳,然后将其与当前交互进行比较。如果两者之间的差异大于您的时间限制,则您将用户注销。一种存档方法是设置一个带有登录哈希的cookie,每次用户请求页面时,该登录哈希的过期时间为X分钟。如果cookie不存在,则用户将注销。这也可以是会话cook

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

如果你能抽出时间,我将不胜感激。
谢谢您保存了上次交互的UNIX时间戳,然后将其与当前交互进行比较。如果两者之间的差异大于您的时间限制,则您将用户注销。

一种存档方法是设置一个带有登录哈希的cookie,每次用户请求页面时,该登录哈希的过期时间为X分钟。如果cookie不存在,则用户将注销。这也可以是会话cookie。当然,只有当你在谈论一个webapp时,这才有效:)

我会记录
-(void)application将进入前台:(UIApplication*)application
-(void)application将进入后台:(UIApplication*)application
的时间,计算不同的时间,检查它是否超过某个值,从上一个会话注销用户。

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

创建自己的UIApplication子类:

@interface MyUIApplication : UIApplication {
  NSTimer *idleTimer;
}
@property(nonatomic,retain) NSTimer *idleTimer;
在你的主要

在应用程序子类.m中,重写sendEvent:和sendAction:。参见苹果文档:

。。。发送操作也是如此。您也可以将注销逻辑放在此类中:

- (void)logout:(NSTimer *)timer {
  // do logout
}
当应用程序辞职时,您仍应在应用程序代理中注销(并使计时器无效):

- (void)applicationWillResignActive:(UIApplication *)application {
  [application logout:nil];
}
//UIC应用程序的扩展方法
-(void)sendEvent:(UIEvent*)事件{
[超级发送事件:事件];
//只想在开始触摸或结束触摸时重置计时器,以减少计时器重置次数。
NSSet*AllTouchs=[event AllTouchs];
如果([AllTouchs计数]>0){
//AllTouchs count only似乎是1,所以anyObject在这里工作。
UITouchPhase=((UITouch*)[AllTouchAnyObject])阶段;
如果(相位==UITouchPhaseBegan | |相位==UITouchPhaseEded){
[自复位IDletimer:否];
}
}
}
-(无效)重置IDletimer:(BOOL)强制{
//不要费心重置计时器,除非上次重置后至少5秒。
//但是如果maxIdleTime值已更改,则需要强制重置。
NSTimeInterval now=[NSDate timeIntervalSinceReferenceDate];
如果(强制| |(现在-lastTimerReset)>5.0){
//调试日志(@“使用值%f重置空闲超时”,maxIdleTime);
lastTimerReset=现在;
//假设任何小于1秒的时间值为零,这意味着禁用计时器。
//句柄值>1秒。
如果(最大空闲时间>1.0){
//如果还没有计时器,创建一个
if(idleTimer==nil){
//创建一个新计时器并保留它。
idleTimer=[[NSTimer scheduledTimerWithTimeInterval:maxIdleTime目标:自选择器:@selector(IDletimeExceed)userInfo:nil repeats:NO]retain];
}
//否则,重置现有计时器的“点火日期”。
否则{
[idleTimer setFireDate:[NSDate DATE WITH TIMEINTERVALICENCENOW:maxIdleTime]];
}
}
//如果maxIdleTime为零(或<1秒),请禁用任何活动计时器。
否则{
如果(空闲时间){
[idleTimer invalidate];
[空闲释放];
idleTimer=nil;
}
}
}
}
-(无效)空闲时间超过{
NSLog(@“超过%f秒的空闲时间限制--正在结束应用程序”,maxIdleTime);
//强制结束
//self.dealloc;--StartProcessViewController.m中有一个dealloc,但我不确定是否应该解除UIApplication对象的锁定。
_出口(0);
}

您可以获取用户单击某物或从后台打开应用程序后的时间,如果超过5分钟,请调用函数logout();我相信,有一种方法可以做到这一点,那就是只需聆听对UIWindow的触摸。但我把说明书忘在我的另一条裤子里了。(事实上,它们在我的工作电脑上。)这是通过扩展UIApplication实现的,我在下面发布了完整的代码。我最初认为JanL方法是可行的,但我必须在我的每个功能中添加这些代码。你能更详细地给我解释一下第一种方法吗?我在这里和其他地方翻了翻,两种方法的结合效果最好。诀窍是捕获UIApplication子类中的事件。我对我的答案进行了相应的编辑。根据您为所有美国拷贝和粘贴程序定义的属性,我认为“logoutTimer”应该是“idleTimer”。非常有帮助!回答得好!现在,如何在另一个viewController中调用方法?我得到[NSObject(NSObject)doesNotRecognizeSelector:]它最终可以从应用程序委托访问:self.window.rootViewController。从这里开始,通常容器视图控制器有方法来挖掘包含的vc(比如UINavigation vc有一个ViewController数组)。我不确定,但我有这个问题。如果用户已登录,但未与应用程序交互,则此方法仍然有效??此方法不适用于应用程序保持运行,因为您在后台请求计数时间转义。若要计算应用程序是否仍在运行,您可以使用计时器,在时间结束时触发注销,并在检测用户交互时重置计时器。
- (void)logout:(NSTimer *)timer {
  // do logout
}
- (void)applicationWillResignActive:(UIApplication *)application {
  [application logout:nil];
}
// 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);
}