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

// 定义未操作通知的时间,也可以从服务器上获取。
#define kApplicationTimeoutInMinutes 30 @interface NTApplication : UIApplication {
NSTimer *_myTimer;
} - (void)resetTimer; @end
@implementation NTApplication

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

    [super sendEvent:event];

    if (!_myTimer) {

        [self resetTimer];

    }
NSSet *allTouches = [event allTouches]; if ([allTouches count] > ) { UITouchPhase phase = ((UITouch *) [allTouches anyObject]).phase; if (phase ==UITouchPhaseBegan) {
[self resetTimer];
} }
[[NSNotificationCenter defaultCenter] postNotificationName:kUserBreakFreeNotification object:nil];
} //重置时钟 - (void)resetTimer { if (_myTimer) { [_myTimer invalidate]; } int timeout = kApplicationTimeoutInMinutes;//超时时间,我这里设置为30s _myTimer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(freeTimerNotificate:) userInfo:nil repeats:NO]; } //当达到超时时间,发送 kApplicationTimeoutInMinutes通知 - (void)freeTimerNotificate:(NSNotification *)notification {
//在想要获得通知的地方注册这个通知就行了
[[NSNotificationCenter defaultCenter] postNotificationName:kUserEnterFreeTimeoutNotification object:nil];
} @end

还有最重要的一部,将NTApplication与当前的AppDelegate关联起来,在main.m中更改

#import "NTApplication.h"

int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([NTApplication class]), NSStringFromClass([AppDelegate class]));
}
}

UIApplicationMain原来的第三个参数是nil,更改成NSStringFromClass([NTApplication class])

iOS实现程序长时间未操作退出的更多相关文章

  1. iOS开发笔记--如何实现程序长时间未操作退出

    我们使用金融软件经常会发现手机锁屏或者长时间未操作就会退出程序或者需要重新输入密码等情况.下面让我们看一下如何实现这种功能.我们知道iOS有一个事件循环机制,也就是大家所说的runloop.我们在对程 ...

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

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

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

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

  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. ASP.NET 工作流:支持长时间运行操作的 Web 应用程序

    ASP.NET 工作流 支持长时间运行操作的 Web 应用程序 Michael Kennedy   代码下载位置:MSDN 代码库 在线浏览代码 本文将介绍以下内容: 独立于进程的工作流 同步和异步活 ...

随机推荐

  1. java内存解析

    ass BirthDate{ private int day; private int month; private int year; public BirthDate(int d;int m,in ...

  2. kali 安装virtualbox

    安装virualbox 的三大步: 1.添加源: Add the following line to your /etc/apt/sources.list: deb http://download.v ...

  3. 学习记录:交叉编译环境配置(buildroot and gdb&gdbserver)【转】

    本文转载自:https://blog.csdn.net/zhy025907/article/details/52332528 1,背景 因为参加公司的路由器逆向培训,首先需要的就是环境的配置准备工作, ...

  4. 移植gdb到海思3716板子的方法【转】

    本文转载自:https://blog.csdn.net/yuhengyue/article/details/78414403 一,移植方法 环境:ubuntu12.04 交叉编译工具链路径:/opt/ ...

  5. System.IO命名空间下常用的类

    System.IO System.IO.Directory 目录 System.IO.Path 文件路径(包含目录和文件名) System.IO.FileInfo 提供创建.复制.删除.移动和打开文件 ...

  6. python sort dict 总结

    python中的dict是不能排序的,只有对dict的representation进行排序,例如list或者tuple 排序肯定会用到sorted函数,那么我们就来讲一下sorted函数. sorte ...

  7. 【cs231n】神经网络学习笔记3

    + mu) * v # 位置更新变了形式 对于NAG(Nesterov's Accelerated Momentum)的来源和数学公式推导,我们推荐以下的拓展阅读: Yoshua Bengio的Adv ...

  8. MySQL级联删除和级联修改

    1.新建主键table create table demo1_zhujian ( id int primary key auto_increment, name )); 2.新建外键table cre ...

  9. 远程线程注入shellcode笔记

    #include "stdafx.h" #include <windows.h> #include <stdio.h> char shellcode[] = ...

  10. maven 本地仓库的配置

    <?xml version="1.0" encoding="UTF-8"?> <!--Licensed to the Apache Softw ...