我知道在 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。
这将有效地增加自动锁定时间。我不认为你可以减少它(即用户有自动锁定从不,你想在一分钟后锁定设备)。
使用以下代码(假设您将 Auto-Lock 设置为 1 分钟),应用将保持唤醒状态 2 分钟,之后我们设置 idleTimerDisabled = NO;
使应用变暗 15 秒它会关闭。
将以下两个文件添加到您的项目中(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)sendEventUIEvent *)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
selectorselector(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
在您的 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]));
}
}
在 AppDelegate.m 中编辑 didFinishLaunchingWithOptions: 方法并添加 applicationDidTimeout: 方法。
- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{
// Override point for customization after application launch.
[UIApplication sharedApplication].idleTimerDisabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selectorselector(applicationDidTimeout
name:kApplicationDidTimeoutNotification object:nil];
return YES;
}
- (void) applicationDidTimeoutNSNotification *) notif {
NSLog(@"applicationDidTimeout");
[UIApplication sharedApplication].idleTimerDisabled = NO;
}
关于ios - 如何为 iOS 手动设置设备的锁定时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21324060/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |