代码看完后感觉非常优秀

http://stackoverflow.com/questions/8085188/ios-perform-action-after-period-of-inactivity-no-user-interaction

1.   新建 Objective-C 类,继承 UIApplication。

2.   编辑 .h 如下:

#import <Foundation/Foundation.h>

// 定义应用程序超时时间,单位为分钟,因此我们会在这个数上乘以60,以便折算成秒数。

#define kApplicationTimeoutInMinutes 5

// 定义通知名称,其真实内容是字符串 "timed out"

#define kApplicationDidTimeoutNotification

@"AppTimeOut"

@interface TIMERUIApplication : UIApplication {

NSTimer     *myidleTimer;

}

-(void)resetIdleTimer;

@end

3. 编辑 .m 如下:

#import "TIMERUIApplication.h"

@implementation TIMERUIApplication

// 监听所有触摸,当屏幕被触摸,时钟将被重置

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

[super sendEvent:event];

if (!myidleTimer) {

[selfresetIdleTimer];

}

NSSet *allTouches = [eventallTouches];

if ([allTouches count] > 0) {

UITouchPhase phase= ((UITouch *)

[allTouchesanyObject]).phase;

if (phase ==UITouchPhaseBegan) {

[self resetIdleTimer];

}

}

}

// 重置时钟

-(void)resetIdleTimer {

if (myidleTimer) {

[myidleTimerinvalidate];

}

// 将超时时间由分钟转换成秒数

int timeout =

kApplicationTimeoutInMinutes* 60;

myidleTimer = [NSTimer

scheduledTimerWithTimeInterval:timeout

target:self

selector:@selector(idleTimerExceeded)

userInfo:nilrepeats:NO];

}

// 当达到超时时间,张贴 kApplicationTimeoutInMinutes 通知

-(void)idleTimerExceeded {

[[NSNotificationCenter defaultCenter]

postNotificationName:

kApplicationDidTimeoutNotification

object:nil];

}

@end

4.   修改 main.m :

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

#import "TIMERUIApplication.h"

int main(int argc, char *argv[]) {

@autoreleasepool {

returnUIApplicationMain(argc, argv,

NSStringFromClass(

[TIMERUIApplicationclass]),

NSStringFromClass(

[AppDelegate

class]));

}

}

5. 接下来编辑 AppDelegate.mfile,不需要编辑 AppDelegate.h。

#import "AppDelegate.h"

#import "TIMERUIApplication.h"

@implementation AppDelegate

@synthesize window = _window;

-(BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions  {

[[NSNotificationCenter defaultCenter]

addObserver:self

selector:

@selector(applicationDidTimeout:)

name:

kApplicationDidTimeoutNotification

object:nil];

return YES;

}

-(void)applicationDidTimeout:(NSNotification *)notif {

NSLog (@"time exceeded!!");

//这是故事板和xib文件不同的地方。对于你想跳转到的 View Controller,确保下面代码中的id 和故事板中 View Controller 的 Storyboard Identifier 一致。在本例中,即"mainView"。而我的故事板文件名为MainStoryboard.storyboard, 确保你的文件名和 storyboardWithName 参数保持一致。

UIViewController *controller =

[[UIStoryboard

storyboardWithName:@"MainStoryboard"

bundle:NULL]

instantiateViewControllerWithIdentifier:

@"mainView"];

[(UINavigationController*)

self.window.rootViewController

pushViewController:controller

animated:YES];

}

提示: 一旦侦测到触摸,定时器会被启动。也就是说,如果用户触摸了主窗口(例如“mainView”),哪怕并没有从主窗口离开,同一个视图仍然会在指定时间后 push。这在我的 app 中不是问题,但对于你的 app 则可能是个问题。

这将导致视图每隔 x 分钟就push 一次。哪怕侦测到触摸,时钟仍然会被重置。

这个问题的一种解决方案是,在app delegate 中声明一个 Bool 成员 idle,这样,当你想侦测用户是否无动作时将其设置为 true,如果仅仅是跳转到 idle view 则设置为false。然后在 TIMERUIApplication 的 idleTimerExceeded 方法中使用如下的 if 语句。在所有你想侦测用户是否无动作的视图中,将app delegate 的 idle 设置为 true。对于不需要侦测用户是否无动作的视图,将 idle 设置为 false。

-(void)idleTimerExceeded{

AppDelegate *appdelegate = [[UIApplication

sharedApplication] delegate];

if(appdelegate.idle){

[[NSNotificationCenter defaultCenter]

postNotificationName:

kApplicationDidTimeOutNotification

object:nil];

}

}

iOS perform action after period of inactivity (no user interaction)的更多相关文章

  1. AuthorizationFailed""The client '***' with object id '***' does not have authorization to perform action 'or the scope is invalid. If access was recently granted, please refresh your credentials

    Warning  SyncLoadBalancerFailed  4m9s (x11 over 29m)   service-controller  Error syncing load balanc ...

  2. An iOS zero-click radio proximity exploit odyssey

    NOTE: This specific issue was fixed before the launch of Privacy-Preserving Contact Tracing in iOS 1 ...

  3. Cisco IOS Security command Guide

    copy system:running-config nvram:startup-config : to save your configuration changes to the startup ...

  4. iOS 逆向之ARM汇编

    最近对iOS逆向工程很感兴趣. 目前iOS逆向的书籍有: <Hacking and Securing IOS Applications>, <iOS Hacker's Handboo ...

  5. iOS安装包瘦身的那些事儿

    在我们提交安装包到App Store的时候,如果安装包过大,有可能会收到类似如下内容的一封邮件: 收到这封邮件的时候,意味着安装包在App Store上下载的时候,有的设备下载的安装包大小会超过100 ...

  6. CallKit iOS 教程

    原文:CallKit Tutorial for iOS 作者:József Vesza 译者:kmyhy 对 VoIP App 开发者来说,iOS 的支持并不友好.尤其是它的通知发送这一块,太糙了.你 ...

  7. 浅谈C#中常见的委托<Func,Action,Predicate>(转)

    一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...

  8. [转]ios push

    转:http://blog.csdn.net/showhilllee/article/details/8631734 APNS的推送机制 首先我们看一下苹果官方给出的对ios推送机制的解释.如下图 P ...

  9. iOS开发者证书申请过程

    真机测试前准备工作:1.苹果的MAC一台.如果你用的是***不知道可不可以,反正我没用过...一般公司都会给你配开发工具的.2.iphone手机一部.(本人纯屌丝,用的iphone4)3.开发者账号. ...

随机推荐

  1. 创建Sitemap文件供搜索引擎使用

    以下内容转载自 http://www.cnblogs.com/webtrados/archive/2009/12/29/1635305.html 如何创建Sitemap文件 Sitemap的格式有XM ...

  2. CCF 201512-1 数位之和 (水题)

    问题描述 给定一个十进制整数n,输出n的各位数字之和. 输入格式 输入一个整数n. 输出格式 输出一个整数,表示答案. 样例输入 20151220 样例输出 13 样例说明 20151220的各位数字 ...

  3. sql #与$的区别

    #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by “111”, 如果传入的值是i ...

  4. C++继承详解:共有(public)继承,私有(private)继承,保护(protected)继承

    公有继承(public).私有继承(private).保护继承(protected)是常用的三种继承方式. 1. 公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时, ...

  5. HDU2852【树状数组+二分】

    额..有点遗忘了树状数组特性了..印象中一直是前缀和,然后一定要记住树状数组是把给出的值(值太大可能可以离散化)也就是点到了区间,然后这个点存的值就是由自己来定了. 题意: 百度. 思路: 树状数组是 ...

  6. HDU1080 【LCS变形】

    题意: 给你每种字符匹配的权值大小,给你两个串,长度小的串可以在小串里面添加空格和大串匹配,问你一个最大匹配权值. 思路: 有点类似于LCS吧,我们在求两个串的LCS的时候,不行的就扔掉了,在这里就是 ...

  7. CodeForces691C 【模拟】

    这一题的模拟只要注意前后导零就好了... 感受就是... 如果是比赛中模拟题打好..要盯着注意点,测试不同的情况下的注意点..起码要针对性测试10分钟.. 还是蛮简单的,但是自己打烦了,应该,队友代码 ...

  8. Linux 根据进程ID查看文件路径(转)

    遇到的问题是想要查看进程的启动脚本在哪里,比如自己写的weblogic启动脚本,但忘记放在哪里了,这时候可以用以下方式 1.用ps -ef |grep xxxxx 得到该进程的pid 2.输入ls - ...

  9. android webview 视频相关

    //设置为false则可以自动播放页面音视频 页面必须设置autoplay或者调用document.getElementById("video").play(); $(" ...

  10. Jquery | 基础 | html()

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...