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



我在iOS中知道,如果您将设备空闲45秒,则屏幕将变暗,如果闲置了15秒,则该设备将自动锁定。

,我们可以通过 [uiapplication sharedApplication] .idletimerDisabled = yes

但是,我确实想要此功能,只想更长的时间,我可以手动设置此计时器吗?

是否有任何方法(没有越狱)?

谢谢

您可以通过监视用户与应用程序(触摸事件)的互动(触摸事件)和设置[UIApplication sharedApplication].idleTimerDisabled = NO;时实现此目标。

您可以在此博客上了解有关监视事件的更多信息。但是,我已经概述了以下步骤,其中包含ARC的代码更新。

测试之后可能的背景。苹果的内部空闲计时器都可以运行,无论是否设置idleTimerDisabled = YES;。这意味着,如果用户未与手机相互作用,则在设置idleTimerDisabled = NO;时(即1分钟),设置设备时,设备将立即变暗,并在15秒后完全关闭。因此,我们可以做的是禁用idletimer,并手动创建一个新的计时器,该计时器等待x分钟,然后再启用脚踏仪。

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

使用以下代码(假设您将自动锁定设置为1分钟),该应用程序将保持清醒2分钟,然后我们将idleTimerDisabled = NO;设置为15秒钟,然后将其关闭。

  1. 将以下两个文件添加到您的项目(原始来源此处):

    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. in appdelegate.m 编辑didfinishlaunchingwithoptions:方法并添加applicationDidTimeOut:method。

    - (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;
    }
    

最新更新