如何在检测到自上次屏幕触摸后的用户不活动/空闲时间时注销应用程序

时间:2017-08-03 04:39:37

标签: ios objective-c iphone

我想在我的应用中添加注销功能,以便用户在上次触摸屏幕后自动注销不活动/空闲时间。

请建议我实现此功能的方法。

有些人告诉你在代码中添加它;

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

    }

但我的问题是在哪里添加此代码。

2 个答案:

答案 0 :(得分:2)

删除所有计时器代码,然后执行此操作。当用户点击时,安排在X秒后调用您的空闲方法(使用performSelector: afterDelay:)。他们点击的任何时候,取消所有预定的请求(使用cancelPreviousPerformRequestsWithTarget:)并请求新的请求X秒。

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

答案 1 :(得分:0)

这是代码段的快速版本,它可以检测屏幕上的空闲时间,并可以触发事件,例如自动注销/ API调用/其他事件调用。转到以下简单步骤

  1. 初始化以下三行代码以设置持续时间和计划时间
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. 使用以下方法编写任何事件的所需代码。您可以根据需要编写自己的方法名称
@objc func callPrintEventAfterTimeExceeded() {
    print("idle time exceeded")
}