ios - 如何为 iOS 手动设置设备的锁定时间
<p><p>我知道在 iOS 中,如果您让设备闲置 45 秒,屏幕会变暗,如果再闲置 15 秒,设备将自动锁定。 </p>
<p>我们可以通过以下方式禁用自动锁定
<strong>.idleTimerDisabled = YES</strong></p>
<p>但是,我确实想要这个功能,只是想让它更长,有什么方法(不越狱)我可以手动设置这个计时器吗?</p>
<p>谢谢</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以在一定程度上通过监控用户与您的应用的交互(触摸事件)并在自定义空闲计时器到期时设置 <code>.idleTimerDisabled = NO;</code> 来实现。</p >
<p>您可以了解有关监控事件的更多信息 <a href="http://www.icodeblog.com/2011/09/19/timing-out-an-application-due-to-inactivity/" rel="noreferrer noopener nofollow">on this blog</a> .不过,我已经通过 ARC 的代码更新概述了以下步骤。</p>
<p>关于测试后可能发生的事情的一些背景知识。无论 <code>idleTimerDisabled = YES;</code> 是否设置,Apple 的内部空闲计时器都会运行。这意味着如果用户在设置 <code>idleTimerDisabled = NO;</code> 时没有与手机交互超过自动锁定设置(即 1 分钟),则设备将立即变暗并在之后完全关闭15 秒。所以我们可以做的是禁用 idleTimer,并手动创建一个新的计时器,它会等待 x 分钟,然后再次启用 idleTimer。</p>
<p>这将有效地增加自动锁定时间。我不认为你可以减少它(即用户有自动锁定从不,你想在一分钟后锁定设备)。</p>
<p>使用以下代码(假设您将 Auto-Lock 设置为 1 分钟),应用将保持唤醒状态 2 分钟,之后我们设置 <code>idleTimerDisabled = NO;</code> 使应用变暗 15 秒它会关闭。</p>
<ol>
<li><p>将以下两个文件添加到您的项目中<a href="https://github.com/B-Sides/ELCUIApplication" rel="noreferrer noopener nofollow">(original source here)</a> :</p>
<p><em>ELCUIApplication.h</em></p>
<pre><code>//
//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
</code></pre>
<p><em>ELCUIApplication.m</em></p>
<pre><code>//
//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 {
;
// Fire up the timer upon first event
if(!_idleTimer) {
;
}
// Check to see if there was a touch event
NSSet *allTouches = ;
if ( > 0) {
UITouchPhase phase = ((UITouch *)).phase;
if (phase == UITouchPhaseBegan) {
;
}
}
}
- (void)resetIdleTimer {
if (_idleTimer) {
;
// ;
}
// 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 */
[
postNotificationName:kApplicationDidTimeoutNotification object:nil];
}
//- (void) dealloc {
//;
//;
//}
@end
</code></pre> </li>
<li><p>在您的 Supporting Files 文件夹中打开 <em>main.m</em>,更新以下内容:</p>
<pre><code>#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv,@"ELCUIApplication", NSStringFromClass());
}
}
</code></pre> </li>
<li><p>在 <em>AppDelegate.m</em> 中编辑 didFinishLaunchingWithOptions: 方法并添加 applicationDidTimeout: 方法。</p>
<pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
.idleTimerDisabled = YES;
[ addObserver:self selector:@selector(applicationDidTimeout:)
name:kApplicationDidTimeoutNotification object:nil];
return YES;
}
- (void) applicationDidTimeout:(NSNotification *) notif {
NSLog(@"applicationDidTimeout");
.idleTimerDisabled = NO;
}
</code></pre> </li>
</ol></p>
<p style="font-size: 20px;">关于ios - 如何为 iOS 手动设置设备的锁定时间,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21324060/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21324060/
</a>
</p>
页:
[1]