iOS开发笔记--如何实现程序长时间未操作退出
2016-06-23 11:14
1046 查看
我们使用金融软件经常会发现手机锁屏或者长时间未操作就会退出程序或者需要重新输入密码等情况。下面让我们看一下如何实现这种功能。我们知道iOS有一个事件循环机制,也就是大家所说的runloop。我们在对程序进行手势操作时、如点击、滑动、长按、双击等都会响应对应的事件。那么我们就可以利用这个原理监听所有的屏幕事件来实现我们的功能。在程序里负责对用户事件进行处理的是UIApplication。那么如果我们想要做点什么的话,就要继承这个类。然后在里面做一些操作。好了,废话说完。下面上代码。
首先创建一个继承UIApplication的类。
SWFUIApplication.h文件
SWFUIApplication.m文件
主要代码写完,下面来看一下如何使用
AppDelegate.m
demo的功能是 模拟登陆页面 然后5秒未操作 会切换另一个页面
控制器代码
实现效果
首先创建一个继承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] > 0) { 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]; }
实现效果
相关文章推荐
- 解决iOS 控制器继承后,控制器侧滑失效的问题
- iOS 常写在pch中的设备区分
- CGImageCreateWithImageInRect图片裁剪问题
- iOS 手势滑动返回功能
- ios::sync_with_stdio(false);(读入优化)
- iOS TableView实现QQ好友列表
- iOS开发:字典(NSDictionary)和数组( NSArray)转换成字符串
- ios webView与js的交互
- OC获取folder下文件的路径
- iOS流媒体工程后台运行
- IOS 中的 _cmd 代表本方法的名称
- iOS 程序间跳转传参(支付和地图)
- cordova local notification sound not working in ios and andorid
- iOS小demo之判断数值类型
- iOS自动更新如何实现
- iOS 修改状态栏的颜色
- iOS-通过键盘的监听完成底部工具条的黏性移动
- iOS 核心动画Core Animation
- 如何用xcode7.3在ios10的真机上调试代码
- iOS开发之优秀开源框架IQKeyboardManager,零行代码解决键盘遮挡问题,如此简单解决键盘遮挡问题,IQKeyboardManager源码分解