ios - NSTimer 在无效时不会停止

标签 ios iphone xcode nstimer

我想检查应用程序是否空闲(用户在过去 30 分钟内未执行任何操作)并注销用户。

为此,我有一个重置计时器的事件管理器。

- (void)sendEvent:(UIEvent *)event {      
    if(event == nil) {
        [self resetIdleTimer];
    } else {
        [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 (self.idleTimer) {
        [self.idleTimer invalidate];
        self.idleTimer = nil;
    }

    NSInteger maxTime = 60*30; //30 minutes

    self.idleTimer = [NSTimer scheduledTimerWithTimeInterval:maxTime target:self selector:@selector(checkIfIdleTimerExceeded) userInfo:nil repeats:NO];
}
- (void)checkIfIdleTimerExceeded {

    theProfileManager = [[ProfileManager alloc] init];
    theProfile = [theProfileManager getProfile];

    if( ! [[theProfile getStatus]isEqualToString:@"u1"]) {
        if( ! [[theProfile getTheUser] isUnlimitedLogin]) {
            theProfile = nil;
            theUserManager = [[UserManager alloc] init];
            [theUserManager setCurrentUser:[theProfile getTheUser]];
            [theUserManager setCurrentUserAccount:[[theProfile getTheUser] getTheUserAccount]];
            [theUserManager setCurrentUserSettings:[[theProfile getTheUser] getTheUserSettings]];
            [theUserManager logoutCurrentUser];

            [[MenuItemDataManager alloc] deleteJsonData];

            NSNotification *msg = [NSNotification notificationWithName:@"leftPanelMsg" object:[[NSString alloc] initWithFormat:@"Home"]];
            [[NSNotificationCenter defaultCenter] postNotification:msg];

            [self performSelector:@selector(loadHomeView:) withObject:nil afterDelay:0.5f];
        }
    }
    [self resetIdleTimer];

}

checkIfIdleTimerExceeded 执行注销过程。

问题:在我触摸屏幕 15 分钟后,resetIdleTime 被调用并且应该重新启动一个新的 NSTimer。但 15 分钟后,应用程序将用户注销。

感谢您的帮助。

安德烈。

最佳答案

根据您对 Touches 事件的要求。你应该这样做:

 NSSet *allTouchEvents = [event allTouches];
    if ([allTouchEvents count] > 0) {
        // allTouchEvents count only ever seems to be 1, so anyObject works here.
        UITouchPhase phase = ((UITouch *)[allTouchEvents anyObject]).phase;
        if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
            [self resetIdleTimer];
    }

而在resetIdleTimer中,

self.idleTimer = [NSTimer scheduledTimerWithTimeInterval:maxTime target:self selector:@selector(checkIfIdleTimerExceeded) userInfo:nil repeats:NO];

应该是这样

self.idleTimer  = [NSTimer scheduledTimerWithTimeInterval:maxTime target:self selector:@selector(checkIfIdleTimerExceeded:) userInfo:nil repeats:NO]; 

checkIfIdleTimerExceeded 应该是这样的:

-(void) checkIfIdleTimerExceeded :(NSTimer*)timer
    {

       //Do your all process and invalidate after completion

        [timer invalidate];

    }

关于ios - NSTimer 在无效时不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24449592/

相关文章:

ios - 由于缺少隐私访问说明,iTunes Connect在上载后拒绝了我的应用程序

ios - 无法在 Swift 中调用 Obj-c 中定义的函数

ios - SpriteKit - 对在多个 SKNode 上运行的多个 SKAction 进行排序

ios - iOS 6 上的 UIButtonTypeSystem

iphone - 使用填充将UIWebView内容居中

iphone - iOS iphone 解压缩跨区(多卷).7z 或 .zip 文件

jquery - iOS Web 应用程序启动时总是带有闪屏

ios - 使用 dispatch_async 时的 EXC_BAD_ACCESS

ios - 无法设置引用 socket

ios - UIcollectionview 单元格问题 Swift