ios - 如何为 iOS 手动设置设备的锁定时间

标签 ios timer system sleep

我知道在 iOS 中,如果您让设备闲置 45 秒,屏幕会变暗,如果再闲置 15 秒,设备将自动锁定。

我们可以禁用自动锁定 [UIApplication sharedApplication].idleTimerDisabled = YES

但是,我确实想要这个功能,只是想让它更长一些,有什么方法(没有越狱)可以让我手动设置这个定时器吗?

谢谢

最佳答案

您可以通过监视用户与您的应用程序的交互(触摸事件)并在自定义空闲计时器到期时设置 [UIApplication sharedApplication].idleTimerDisabled = NO; 来在一定程度上实现这一点。

您可以了解有关监控事件的更多信息on this blog .不过,我在下面概述了 ARC 代码更新的步骤。

一些关于测试后可能发生的事情的背景知识。无论是否设置了 idleTimerDisabled = YES;,Apple 的内部空闲计时器都会运行。这意味着如果用户在设置 idleTimerDisabled = NO; 时没有与手机互动超过自动锁定设置(即 1 分钟),设备将立即变暗并在之后完全关闭15 秒。所以我们可以做的是禁用 idleTimer,并手动创建一个新计时器,它会等待 x 分钟,然后再次启用 idleTimer。

这将有效地允许您增加自动锁定时间。我不认为你可以减少它(即用户有自动锁定从不,你想在一分钟后锁定设备)。

使用以下代码(假设您将自动锁定设置为 1 分钟)应用程序将保持唤醒状态 2 分钟,之后我们设置 idleTimerDisabled = NO; 将应用程序调暗 15 秒它会关闭。

  1. 将以下两个文件添加到您的项目中(original source here) :

    ELCUIApplication.h

    //
    //  ELCUIApplication.h
    //
    //  Created by Brandon Trebitowski on 9/19/11.
    //  Copyright 2011 ELC Technologies. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    // # of minutes before application times out
    #define kApplicationTimeoutInMinutes 2
    
    // Notification that gets sent when the timeout occurs
    #define kApplicationDidTimeoutNotification @"ApplicationDidTimeout"
    
    /**
     * This is a subclass of UIApplication with the sendEvent: method 
     * overridden in order to catch all touch events.
     */
    
    @interface ELCUIApplication : UIApplication {
        NSTimer *_idleTimer;
    }
    
    /**
     * 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
    

    ELCUIApplication.m

    //
    //  ELCUIApplication.m
    //
    //  Created by Brandon Trebitowski on 9/19/11.
    //  Copyright 2011 ELC Technologies. All rights reserved.
    //
    
    #import "ELCUIApplication.h"
    
    @implementation ELCUIApplication
    
    - (void)sendEvent:(UIEvent *)event {
        [super sendEvent:event];
    
        // Fire up the timer upon first event
        if(!_idleTimer) {
            [self resetIdleTimer];
        }
    
        // Check to see if there was a touch event
        NSSet *allTouches = [event allTouches];
        if ([allTouches count] > 0) {
            UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
            if (phase == UITouchPhaseBegan) {
                [self resetIdleTimer];         
            }
        }
    }
    
    - (void)resetIdleTimer {
        if (_idleTimer) {
            [_idleTimer invalidate];
    //        [_idleTimer release];
        }
    
        // Schedule a timer to fire in kApplicationTimeoutInMinutes * 60
        float timeout = kApplicationTimeoutInMinutes * 60;
        _idleTimer = [NSTimer scheduledTimerWithTimeInterval:timeout
                                                      target:self 
                                                    selector:@selector(idleTimerExceeded) 
                                                    userInfo:nil 
                                                     repeats:NO];
    }
    
    - (void)idleTimerExceeded {
        /* Post a notification so anyone who subscribes to it can be notified when
         * the application times out */ 
        [[NSNotificationCenter defaultCenter]
         postNotificationName:kApplicationDidTimeoutNotification object:nil];
    }
    
    //- (void) dealloc {
    //  [_idleTimer release];
    //  [super dealloc];
    //}
    
    @end
    
  2. 在您的 Supporting Files 文件夹中打开 main.m,更新以下内容:

    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"
    
    int main(int argc, char *argv[])
    {
        @autoreleasepool {
            return UIApplicationMain(argc, argv,  @"ELCUIApplication", NSStringFromClass([AppDelegate class]));
        }
    }
    
  3. AppDelegate.m 中编辑 didFinishLaunchingWithOptions: 方法并添加 applicationDidTimeout: 方法。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // Override point for customization after application launch.
        [UIApplication sharedApplication].idleTimerDisabled = YES;
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidTimeout:)
                                                 name:kApplicationDidTimeoutNotification object:nil];
        return YES;
    }
    
    - (void) applicationDidTimeout:(NSNotification *) notif {
        NSLog(@"applicationDidTimeout");
        [UIApplication sharedApplication].idleTimerDisabled = NO;
    }
    

关于ios - 如何为 iOS 手动设置设备的锁定时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21324060/

相关文章:

ios - 关于 macOS 10.11 上的 URL 编码

javascript - 使用计时器在 Javascript 中使用参数设置动画

java - .nextLine() 直接跳转到下一个输出

linux - 缺少 '=' 。在 Debian 服务中

ios - 无论 Apple 应用程序站点关联如何,通用链接始终打开应用程序

html - Cordova iOS <a href ="mailto:"和 <a href ="tel:"仅适用于长按

javascript - 计时器如何在 Javascript 中工作?

perl - 将参数传递给调试器中的 perl 文件并在系统执行的文件中设置断点

ios - 当我单击 uitableview 单元格时,该单元格的文本变得更粗?

android - 如何在 Android 中启动增量定时器