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

时间:2014-01-24 03:50:01

标签: ios timer system sleep

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

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

然而,我确实想要这个功能,只是想让它更长,有没有任何方法(没有越狱),我可以手动设置这个计时器?

感谢

1 个答案:

答案 0 :(得分:1)

您可以通过监控用户与您的应用的互动(触摸事件)并在自定义空闲计时器到期时设置[UIApplication sharedApplication].idleTimerDisabled = NO;来实现此目的。

您可以详细了解监控事件on this blog。但是,我已经通过ARC的代码更新概述了以下步骤。

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

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

使用以下代码(假设您将自动锁定设置为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. 在您的支持文件文件夹中打开 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;
    }