在 iOS 5.1 中禁用自动屏幕锁定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9904306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable automatic screen lock in iOS 5.1
提问by Selvin
The following line of code prevents the app from automatically locking the screen after some idle time.
以下代码行可防止应用程序在闲置一段时间后自动锁定屏幕。
[UIApplication sharedApplication].idleTimerDisabled = YES; //write this in applicationDidFinishLaunching
It works well till iOS 5.0. But iOS 5.1 does not respect this line and locks the screen after some idle time. How to solve this irritating issue?
它运行良好,直到 iOS 5.0。但 iOS 5.1 不尊重这条线并在一些空闲时间后锁定屏幕。如何解决这个恼人的问题?
Thanks.
谢谢。
Edit:
编辑:
The same code works fine when its installed in 5.0.1 device. But I dont know why it is not working with 5.1 device.
安装在 5.0.1 设备中时,相同的代码工作正常。但我不知道为什么它不适用于 5.1 设备。
采纳答案by Aki
Just setting [UIApplication sharedApplication].idleTimerDisabled = YES;
in
只设置[UIApplication sharedApplication].idleTimerDisabled = YES;
在
- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
works well for me. However, there is a caveat. I have noticed that every time I invoke camera utility to take a snapshot, idleTimerDisablegets set to NObehind the scene. So right after I upload my image, I had to call the following line of code again:
对我来说效果很好。但是,有一个警告。我注意到每次我调用相机实用程序拍摄快照时,idleTimerDisable在幕后都会设置为NO。因此,在我上传图像后,我不得不再次调用以下代码行:
[UIApplication sharedApplication].idleTimerDisabled = YES;
I would not be surprised if there are more places throughout that require same strategy. So far this approach has worked without issues for me.
如果有更多的地方需要相同的策略,我不会感到惊讶。到目前为止,这种方法对我来说没有问题。
回答by orkoden
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
worked for me on iOS 5.1
在 iOS 5.1 上为我工作
回答by calimarkus
No there shouldbe no difference. Perhaps you have another mistake..
See iOS 5.0 to 5.1 API Diffs
不应该没有区别。也许你有另一个错误..
见iOS 5.0 到 5.1 API 差异
回答by jacekmigacz
Important:You should set this property only if necessary and should be sure to reset it to NO when the need no longer exists. Most applications should let the system turn off the screen when the idle timer elapses. This includes audio applications. With appropriate use of Audio Session Services, playback and recording proceed uninterrupted when the screen turns off. The only applications that should disable the idle timer are mapping applications, games, or similar programs with sporadic user interaction.
重要提示:您应该仅在必要时设置此属性,并确保在不再需要时将其重置为 NO。大多数应用程序应该让系统在空闲计时器结束时关闭屏幕。这包括音频应用程序。通过适当使用音频会话服务,屏幕关闭时播放和录制不会中断。应该禁用空闲计时器的唯一应用程序是映射应用程序、游戏或具有零星用户交互的类似程序。
Maybe You exceeds the allowable time limit of being awake?
也许你超出了允许的清醒时间限制?
回答by ParisNakitaKejser
i know its is old, but i found this good and in Swift you can do it look a like this
我知道它很旧,但我发现这很好,在 Swift 中你可以做到这样
application.idleTimerDisabled = true
application.idleTimerDisabled = true
Thanks for you answers! i use right now xcode 7 Beta 3 ( Swift 2 )
谢谢你的回答!我现在使用 xcode 7 Beta 3 ( Swift 2 )
回答by Werner Kratochwil
For Swift, I use this to do outside of delegate:
对于 Swift,我使用它在委托之外执行:
UIApplication.sharedApplication().idleTimerDisabled = true
回答by tomislav
Works fine if your application is registered for some background task, for example GPS location updating.
如果您的应用程序注册了某些后台任务,例如 GPS 位置更新,则工作正常。