40

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?

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.

Selvin
  • 12,333
  • 17
  • 59
  • 80
  • 2
    that sounds like a bug. If you like to report bugs -> http://bugreport.apple.com/ – Matthias Bauch Mar 28 '12 at 09:12
  • Where are you calling this code? Are you sure it is being executed (have you set a breakpoint and checked)? Apple's example is to put `application.idleTimerDisabled = YES;` in the `applicationDidFinishLaunching` of the delegate – Nick Bull Mar 28 '12 at 09:12
  • Yes @NickBull i had put the code inside applicationDidFinishLaunching only. But as you can see, the same code works fine when its installed in 5.0.1 device. – Selvin Mar 28 '12 at 09:20
  • You should be using `application:didFinishLaunchingWithOptions:` – Nick Bull Mar 28 '12 at 09:37
  • My bad. I was actually using `application:didFinishLaunchingWithOptions:` only. I should'nt have confused. – Selvin Mar 28 '12 at 09:48
  • 2
    @MatthiasBauch I filed a bug report to apple... – Selvin Mar 28 '12 at 10:21
  • 1
    Have you tried taking one of Apple's example applications and built and run that? – Nick Bull Mar 28 '12 at 10:31

7 Answers7

25
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];

worked for me on iOS 5.1

orkoden
  • 18,946
  • 4
  • 59
  • 50
25

Just setting [UIApplication sharedApplication].idleTimerDisabled = YES; in

- (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, idleTimerDisable gets set to NO behind the scene. So right after I upload my image, I had to call the following line of code again:

[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.

Aki
  • 3,709
  • 2
  • 29
  • 37
  • 1
    I don't really need idleTimerDisabled = YES in the whole app. You can just set up [[UIApplication sharedApplication] setIdleTimerDisabled: YES]; where you code need it and turn the property off when is no more necessary with [[UIApplication sharedApplication] setIdleTimerDisabled: NO]; – Gabriel.Massana Nov 05 '13 at 14:17
5

No there should be no difference. Perhaps you have another mistake..
See iOS 5.0 to 5.1 API Diffs

calimarkus
  • 9,955
  • 2
  • 28
  • 48
3

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.

Maybe You exceeds the allowable time limit of being awake?

jacekmigacz
  • 789
  • 12
  • 22
  • My application is definitely one with sporadic user interaction ;) but see the code is working in 5.0.1 without any issue. That strikes odd. – Selvin Mar 28 '12 at 09:25
1

i know its is old, but i found this good and in Swift you can do it look a like this

application.idleTimerDisabled = true

Thanks for you answers! i use right now xcode 7 Beta 3 ( Swift 2 )

ParisNakitaKejser
  • 12,112
  • 9
  • 46
  • 66
1

For Swift, I use this to do outside of delegate:

    UIApplication.sharedApplication().idleTimerDisabled = true
0

Works fine if your application is registered for some background task, for example GPS location updating.

tomislav
  • 339
  • 3
  • 6