我们使用金融软件经常会发现手机锁屏或者长时间未操作就会退出程序或者需要重新输入密码等情况。下面让我们看一下如何实现这种功能。我们知道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

iOS开发笔记--如何实现程序长时间未操作退出的更多相关文章

  1. iOS实现程序长时间未操作退出

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

  2. WPF程序长时间无人操作

    在软件开发中为了安全性,特别是那些需要用到用户名和密码登录服务端的程序,常常考虑长期无人操作,程序自动跳转到用户登录界面. 判断程序是否长时间无人操作,有两个依据,第一个是鼠标长时间不动,第二个是鼠标 ...

  3. web页面长时间未操作自动退出登录

    var lastTime = new Date().getTime(); var currentTime = new Date().getTime(); * * ; //设置超时时间: 10分 $(f ...

  4. JavaScript长时间未操作自动退出登录

    主要是通过mouseover 来监听有没有进行当前页面操作,通过未操作时间和设定退出的时间做比较,从而退出登录. var oldTime = new Date().getTime(); var new ...

  5. vue项目前端限制页面长时间未操作超时退出到登录页

    之前项目超时判断是后台根据token判断的,这样判断需要请求接口才能得到返回结果,这样就出现页面没有接口请求时还可以点击,有接口请求时才会退出 现在需要做到的效果是:页面超过30分钟未操作时,无论点击 ...

  6. Android实现app长时间未操作时自动退出app

    这里要考虑3个问题,第一个是锁屏问题,第二个是app被切换至后台的问题,第三个是屏幕锁定和解除时app在后台时的问题 一,监听屏幕解锁,锁定 ? 1 2 3 4 5 6 7 8 9 10 11 12 ...

  7. WPF窗口长时间无人操作鼠标自动隐藏

    在软件开发中有时会有等待一段时间无人操作后隐藏鼠标,可能原因大致如下: 1.为了安全性,特别是那些需要用到用户名和密码登录服务端的程序,常常考虑长期无人操作,程序自动跳转到用户登录界面: 2.软件为了 ...

  8. jsp+js完成用户一定时间未操作就跳到登录页面

    <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" ...

  9. iOS开发笔记7:Text、UI交互细节、两个动画效果等

    Text主要总结UILabel.UITextField.UITextView.UIMenuController以及UIWebView/WKWebView相关的一些问题. UI细节主要总结界面交互开发中 ...

随机推荐

  1. http://blog.csdn.net/szwangdf/article/details/23432783

    http://blog.csdn.net/szwangdf/article/details/23432783

  2. Java笔记6:多态

    一.多态的分类对象的多态性:动物 x = new 猫();函数的多态性:函数重载.重写 二.多态的体现父类的引用指向了自己的子类对象父类的引用也可以接收自己的对象 三.多态的前提必须是类与类之间只有关 ...

  3. Hadoop之Hbase详解

    1.什么是Hbase HBASE是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统, hbase是列式的分布式数据库 1.2.HBASE优势: 1)线性扩展,随着数据量增多可以通过节点扩展进行支撑 ...

  4. 【Linux】pv vg lv, 加盘,扩容磁盘

    PV VG LV关系:一个物理盘(或一个lun)就是一个pv,有几个物理盘就有几个pv.一个或者几个硬盘可以组成一个vg,一个系统可以包括好几个vg,比如rootvg ,datavg等 PV组成VG, ...

  5. Excel 对应.xml/.ftl 配置(中爆导出范文)

    <?xml version="1.0"?><Workbook xmlns="urn:schemas-microsoft-com:office:sprea ...

  6. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何配置虚拟轴 TC3

    在Motion上添加一个NC Task   在Axis上右击添加一个轴,类型为Continuous Axis   在PLC上右击添加新项,然后添加一个PLC项目   在引用中添加TC2_MC2的库引用 ...

  7. [转载]linux 更新yum源 改成阿里云源

    原文链接:https://www.cnblogs.com/bincoding/p/7892762.html 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc ...

  8. js遮罩层弹出显示效果组件化

    1.在web开发中经常遇到遮罩层的效果,可以将这种常用方法通用化 function showid(idname){ var isIE = (document.all) ? true : false; ...

  9. 【CODEFORCES】 C. Table Decorations

    C. Table Decorations time limit per test 1 second memory limit per test 256 megabytes input standard ...

  10. 【CODEFORCES】 C. Captain Marmot

    C. Captain Marmot time limit per test 1 second memory limit per test 256 megabytes input standard in ...