我在我的appdelegate的applicationDidFinishLaunching:方法中使用它来确保iPhone在应用程序打开期间不会进入睡眠状态
[application setIdleTimerDisabled:YES];
Run Code Online (Sandbox Code Playgroud)
它适用于所有屏幕,但在其中一个屏幕上,iPhone会进入睡眠状态.我无法弄清楚如何重现这一点,它似乎随机发生.
有人可以告诉我如何处理这种情况.
谢谢
Ken*_*ker 13
我使用以下方法在整个应用中设置和取消设置此属性:
[UIApplication sharedApplication].idleTimerDisabled = YES;
Run Code Online (Sandbox Code Playgroud)
设置这个你遇到麻烦的地方可以解决它,虽然这可能是一个创可贴的解决方案.
Mik*_*ill 11
值得庆幸的是,这个问题是在5年前发布的,Xcode从那时起取得了突飞猛进的发展.
然而......在2015年,使用Xcode 6.2和iOS 8.2,这个bug仍然存在并且仍然存在.
这是您实际需要做的事情,以防止您的设备进入睡眠状态.
(拿一杯啤酒,这很痛苦.)
当我的应用程序首次在设备上运行时,它会从Web服务加载一堆数据.但是,如果运行时间太长,屏幕会褪色,然后关闭,设备会锁定,我的网页请求将以丑陋的" 网络连接丢失 "错误终止.
我试图添加代码来简单地设置/取消设置idleTimerDisabled值,但是这个更改不会持续很长时间,并且设备仍会在一段时间后自动锁定.
// This didn't work for me (for very long !)
[UIApplication sharedApplication].idleTimerDisabled = NO;
[UIApplication sharedApplication].idleTimerDisabled = YES;
Run Code Online (Sandbox Code Playgroud)
相反,我需要做的事(郁闷叹息......)是每20秒设置一次定时器/取消设置此值.
在我的.h文件中:
@property (strong, nonatomic) NSTimer* stayAliveTimer;
-(void)callEveryTwentySeconds;
Run Code Online (Sandbox Code Playgroud)
在我的.m文件中:
-(void)callEveryTwentySeconds
{
// DON'T let the device go to sleep during our sync
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
}
-(void)loadDataFromWebService
{
self.stayAliveTimer = [NSTimer scheduledTimerWithTimeInterval:20.0
target:self
selector:@selector(callEveryTwentySeconds)
userInfo:nil
repeats:YES];
//
// Code to call our web service in a background thread and wait
// for it to finish (which might take a few minutes)
//
// Kill off our "Stay alive" timer, and allow the device to Auto Lock whenever it wants.
[self.stayAliveTimer invalidate];
// Give our device permission to Auto-Lock when it wants to again.
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}
Run Code Online (Sandbox Code Playgroud)
真的,Apple?
这是2015年,而Xcode 真的还是这么糟糕......?
我希望这段代码可以帮助其他Xcode受害者.
小智 9
试着用
[UIApplication sharedApplication].idleTimerDisabled = NO;
[UIApplication sharedApplication].idleTimerDisabled = YES;
Run Code Online (Sandbox Code Playgroud)
代替
[UIApplication sharedApplication].idleTimerDisabled = YES;
Run Code Online (Sandbox Code Playgroud)
有趣的黑客,但它的工作原理
另见自从iPhone 3.0以来线程idleTimerDisabled无效
| 归档时间: |
|
| 查看次数: |
16811 次 |
| 最近记录: |