我们使用金融软件经常会发现手机锁屏或者长时间未操作就会退出程序或者需要重新输入密码等情况。下面让我们看一下如何实现这种功能。我们知道iOS有一个事件循环机制,也就是大家所说的runloop。我们在对程序进行手势操作时、如点击、滑动、长按、双击等都会响应对应的事件。那么我们就可以利用这个原理监听所有的屏幕事件来实现我们的功能。在程序里负责对用户事件进行处理的是UIApplication。那么如果我们想要做点什么的话,就要继承这个类。然后在里面做一些操作。好了,废话说完。下面上代码。
首先创建一个继承UIApplication的类。
SWFUIApplication.h文件
#import <UIKit/UIKit.h> // 定义未操作的时间,也可以从服务器上获取。
#define kApplicationTimeoutInMinutes 5 // 超时通知名字
#define kApplicationDidTimeoutNotification @"ApplicationDidTimeout" /**
* This is a subclass of UIApplication with the sendEvent: method
* overridden in order to catch all touch events.
*/ @interface SWFUIApplication : UIApplication
{
NSTimer *_myidleTimer;
}
/**
* Resets the idle timer to its initial state. This method gets called
* every time there is a touch on the screen. It should also be called
* when the user correctly enters their pin to access the application.
*/
- (void)resetIdleTimer;
@end
SWFUIApplication.m文件
#import "SWFUIApplication.h" @implementation SWFUIApplication
-(void)sendEvent:(UIEvent *)event { [super sendEvent:event]; if (!_myidleTimer) { [self resetIdleTimer]; }
NSSet *allTouches = [event allTouches]; if ([allTouches count] > ) { UITouchPhase phase= ((UITouch *) [allTouches anyObject]).phase; if (phase ==UITouchPhaseBegan) {
[self resetIdleTimer];
} } } //重置时钟 -(void)resetIdleTimer { if (_myidleTimer) { [_myidleTimer invalidate]; } //将超时时间由分钟转换成秒数 int timeout = kApplicationTimeoutInMinutes* 60; _myidleTimer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO]; } //当达到超时时间,发送 kApplicationTimeoutInMinutes通知 -(void)idleTimerExceeded { [[NSNotificationCenter defaultCenter] postNotificationName:kApplicationDidTimeoutNotification object:nil];
}
@end
主要代码写完,下面来看一下如何使用
AppDelegate.m
#import "AppDelegate.h"
#import "SWFUIApplication.h"
#import "SecondViewController.h"
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidTimeout:) name:kApplicationDidTimeoutNotification object:nil];
return YES;
} -(void)applicationDidTimeout:(NSNotification *)notif {
NSLog (@"超时要进行的操作");
self.window.rootViewController = [SecondViewController new];
[[NSNotificationCenter defaultCenter] removeObserver:self name:kApplicationDidTimeoutNotification object:nil];
}
demo的功能是 模拟登陆页面 然后5秒未操作 会切换另一个页面
控制器代码
#import "ViewController.h"
#import "ThirdViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)btnAction {
[self presentViewController:[ThirdViewController new] animated:YES completion:nil];
}
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
}
#import "ThirdViewController.h" @interface ThirdViewController () @end @implementation ThirdViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor purpleColor];
}
实现效果
DemoGithub地址:https://github.com/yanhuaxuanlan/untreatedExitDemo