iOS实现程序长时间未操作回到登录界面

来源:互联网 发布:蚌埠学院网络选课 编辑:程序博客网 时间:2024/05/29 17:38

大部分银行客户端都有这样的需求,在用户一定时间内未操作,即认定为token失效,但未操作是任何判定的呢?我的想法是用户未进行任何touch时间,原理就是监听runloop事件。我们需要进行的操作是创建一个UIApplication的子类,废话不多说,上代码

#import <UIKit/UIKit.h>


// 定义未操作通知的时间,也可以从服务器上获取。

#define kApplicationTimeoutInMinutes 60


@interface NTApplication : UIApplication {

    NSTimer *_myTimer;

}


- (void)resetTimer;


@end


#import "NTApplication.h"


@implementation NTApplication


- (void)sendEvent:(UIEvent *)event {

    

    [super sendEvent:event];

    

    if (!_myTimer) {

        

        [self resetTimer];

        

    }

    NSSet *allTouches = [event allTouches];

    

    if ([allTouches count] > 0) {

        

        UITouchPhase phase = ((UITouch *)

                              

                              [allTouches anyObject]).phase;

        

        if (phase ==UITouchPhaseBegan) {

            [self resetTimer];

        }

        

    }

}


//重置时钟


- (void)resetTimer {

    

    if (_myTimer) {

        

        [_myTimer invalidate];

        

    }

    

    int timeout =kApplicationTimeoutInMinutes;//超时时间,

    

    _myTimer = [NSTimerscheduledTimerWithTimeInterval:timeouttarget:selfselector:@selector(freeTimerNotificate:)userInfo:nilrepeats:NO];

    

}


//当达到超时时间,发送 kApplicationTimeoutInMinutes通知

- (void)freeTimerNotificate:(NSNotification *)notification {

    //在想要获得通知的地方注册这个通知就行了

    [[NSNotificationCenterdefaultCenter]postNotificationName:@"kUserEnterFreeTimeoutNotification"object:nil];

}




BaseViewController添加此监听 执行相对硬的操作

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(goDengLu:)name:@"kUserEnterFreeTimeoutNotification"object:nil];




原创粉丝点击