`
zl4393753
  • 浏览: 340249 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

iPhone: Detecting user inactivity/idle time since last screen touch

 
阅读更多
iPhone: Detecting user inactivity/idle time since last screen touch

Has anybody implemented a feature where if the user has not touched the screen for a certain time period, you take a certain action? I'm trying to figure out the best way to do that.

There's this somewhat-related method in UIApplication:
[UIApplication sharedApplication].idleTimerDisabled;


It'd be nice if you instead had something like this:
NSTimeInterval timeElapsed = [UIApplication sharedApplication].idleTimeElapsed;


Then I could set up a timer and periodically check this value, and take some action when it exceeds a threshold.

Hopefully that explains what I'm looking for. Has anyone tackled this issue already, or have any thoughts on how you would do it? Thanks.

==========================================

Here's the answer I had been looking for:

Have your application delegate subclass UIApplication. In the implementation file, override the sendEvent: method like so:
- (void)sendEvent:(UIEvent *)event {
    [super sendEvent:event];

    // Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
    NSSet *allTouches = [event allTouches];
    if ([allTouches count] > 0) {
        // allTouches count only ever seems to be 1, so anyObject works here.
        UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
        if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
            [self resetIdleTimer];
    }
}

- (void)resetIdleTimer {
    if (idleTimer) {
        [idleTimer invalidate];
        [idleTimer release];
    }

    idleTimer = [[NSTimer scheduledTimerWithTimeInterval:maxIdleTime target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO] retain];
}

- (void)idleTimerExceeded {
    NSLog(@"idle time exceeded");
}


where maxIdleTime and idleTimer are instance variables.

In order for this to work, you also need to modify your main.m to tell UIApplicationMain to use your delegate class (in this example, AppDelegate) as the principal class:
int retVal = UIApplicationMain(argc, argv, @"AppDelegate", @"AppDelegate");





分享到:
评论

相关推荐

    The Art of Memory Forensic

    The Art of Memory Forensics: Detecting Malware and Threats in Windows, Linux, and Mac Memory Memory forensics provides cutting edge technology to help investigate digital attacks Memory forensics is ...

    CornerNet-Detecting Objects as Paired Keypoints论文的仿真实现代码

    CornerNet是一种创新的目标检测算法,由Huang等人在2018年的论文《CornerNet: Detecting Objects as Paired Keypoints》中提出。该算法引入了一种全新的视角来处理物体检测问题,它将每个物体视为一对关键点,即左上...

    基于SSVEP-BCI的脑机音乐接口系统,可以让用户使用眼睛注视选择音符,再由AI拓展生成音乐

    A comparison study of canonical correlation analysis based methods for detecting steady-state visual evoked potentials[J]. PloS one, 2015, 10(10): e0140703.https://github.com/mnakanishi/12JFPM_S

    Debugging Malloc Lab: Detecting Memory-Related Errors

    errorfl() obviously cannot be used on situations where FREE() is called on an unallocated block, since it was not ever MALLOC'd. Note: You will only be reporting errors from MyFree(). None of the ...

    SURF: Detecting and Measuring Search Poisoning

    ### 相关知识点 #### 一、搜索中毒(Search Poisoning)概述 - **定义**:搜索中毒是一种黑帽SEO技术,它通过操纵搜索引擎结果页面(SERPs),将用户引向恶意网站。与传统的黑帽SEO策略不同的是,搜索中毒并不关注...

    golangcookbook.github.io:Golang 食谱

    去食谱 来源,一个社区为现实世界的 Golang 开发构建并贡献了实用... - title: Detecting a Substring path: /chapters/strings/detecting - title: Detecting All Substrings path: false 指定wip: true将“[正在

    JAVA gc日志分析工具GChisto及CMS GC补丁

    detecting Full GCs corrected parsing errors when using -XX:+PrintGCDateStamps corrected parsing errors when icms_dc=NN appears in GC-log tested so far only with this combination of parameters: -XX:+...

    Detecting Text in Natural Image with Connectionist Text Proposal Network

    提出了一种新颖的连接文本提议网络(CTPN),它能够准确定位自然图像中的文本行。CTPN直接在卷积特征映射中的一系列细粒度文本提议中检测文本行。我们开发了一个垂直锚点机制,联合预测每个固定宽度提议的位置和文本...

    Tunnel Hunter

    Tunnel Hunter: Detecting application-layer tunnels with statistical fingerprinting

    ASE 2013-International Conference on Automated Software Engineering 2013

    ======================= 特别提醒大家: 由于我这里网络条件的原因,往往不能顺利上传到CSDN上...中科院软件所张健老师团队有一篇文章Characterizing and Detecting Resource Leaks in Android Applications被录用。

    优秀毕设基于TensorFlow+OpenCV的焊缝识别

    基于TensorFlow+OpenCV的焊缝识别 文章地址: https://blog.csdn.net/weixin_53403301/article/details/124505827

    CornerNet Detecting Objects as Paired Keypoints

    CornerNet Detecting Objects as Paired Keypoints CornerNet作为配对关键点检测对象 提出了CornerNet,一个新的对象检测方法在检测物体的包围盒作为一对特征点,左上角和右下角,使用一个单一的卷积神经网络,通过...

    Detecting Privacy Leaks in iOS Applications

    通过研究和分析超过1400个iPhone应用程序,PiOS发现大多数应用在尊重用户设备上存储的个人可识别信息方面表现出良好的行为,即使是在非官方仓库(如Cydia)上托管并且只能在越狱手机上运行的应用也不例外。...

    apktool documentation

    move %USERPROFILE%\apktool %USERPROFILE%\AppData\Local v2.0.1 -> v2.0.2 Update apktool to v2.0.2 Remove framework file $HOME/apktool/framework/1.apk due to internal API update (Android ...

    android-x86下载安装问题解决方法.pdf

    当系统在启动时卡在“Detecting Android……”这一步,通常是因为无法找到Android的系统文件。可以通过在启动选项中增加`SRC=/android`,并指定正确的安装分区(例如`root (hd0,0)`)来解决。具体的`root`参数应根据...

    Detecting chaos in pseudoperiodic time series without embedding.pdf

    本文档的标题及描述均为“Detecting chaos in pseudoperiodic time series without embedding.pdf”,简而言之,该研究关注的是如何在不进行相空间重构的情况下检测伪周期时间序列中的混沌现象。伪周期时间序列是指...

    The Zombie Roundup: Understanding, Detecting, and Disrupting Botnets

    僵尸网络,也称为Botnet,是互联网安全领域的一大威胁,由大量被恶意软件感染的设备组成,这些设备在黑客的远程控制下进行恶意活动。这些受控设备,或称为“肉鸡”,可能位于家庭、学校、企业甚至政府机构中,它们在...

    matlab导入excel代码-utl_detecting_structural_breaks_in_a_time_series:检测时间序列

    matlab导入excel代码utl_detecting_structural_breaks_in_a_time_series 检测时间序列中的结构性断裂。 关键字:sas sql join合并大数据分析宏oracle teradata mysql sas社区stackoverflow statistics人工智慧AI ...

    一个奇葩的最短的 IE 版本判断JS脚本

    这是作者博客上写的: 代码如下:// ———————————————————-// A short snippet for detecting versions of IE in JavaScript// without resorting to user-agent sniffing// ——————————...

Global site tag (gtag.js) - Google Analytics