Code Snippet: Prevent The iPhone From Sleeping

  • Twitter
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
July 22nd, 2009 Posted by: (ELC) - posted under:Snippets

The code below will prevent the iPhone from dimming its screen and ultimately going to sleep. Use it wisely as you don’t want your application becoming notorious for being a battery hog :)

[UIApplication sharedApplication].idleTimerDisabled = YES;

Happy iCoding!

  • http://www.appstoremod.com AppStoreMod

    Great Post ! Can you guys do a tutorial on Twitter API ? Also how to save images to your photos from an app.

    Thanks,
    AppStoreMod

  • http://brandontreb.com Brandon

    We do have a Twitter API tutorial here

  • http://clickedapps.com Brian

    @Brandon, I think he means a login also O.o

  • http://idea.wirus.com Jozero

    Been struggling with ‘idleTimerDisabled’ for a while. I’m building a timer that goes off at a set time, at that time the device vibrates ( AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) ). If I set that flag the device doesn’t go to sleep. If its plugged in and I press the lock button at top the display sleeps, I can see in xcode my NSTimer check is still firing, and at the right time the device vibrates.

    However if the device is not plugged in, and I hit the lock button when it hits the set time the device does NOT vibrate. And I can’t figure it out. I know its possible because apps like Night Stand work after you press the lock button.

    I read about a hack that if you play a silent sound continuously the device is active even when lock is pressed, but that seems like a battery devouring hack.

    What am I missing? Any help would be greatly appreciated.

  • http://smallte.ch Santiago Lema

    Playing a 1sec silent sound every 10-15 secs does work. The other processes will continue to run as long as this goes on . That’s how I did it in my alarm clock app ( CuteClock ). It surely eats a lot of battery (as opposed to letting the phone lock itself) but definitely much less than leaving the screen on.

  • http://idea.wirus.com Jozero

    Ahhhh thank you Santiago. That was driving me nuts for a while. For anyone else reading this the sound file must be one second or over, otherwise it won’t work.

    Also thanks for this blog :) There have been a few posts on this issue on various problems including Apples (an Apple engineer did post ‘play a sound’ without much elaboration). Finally the answer is delivered here.

    You have my 99 cents Santiago ! :)

  • http://genericwpthemes.com WP Themes

    I think the dispatch is gifted and on the point. This send really helped me in my assignment.

blog comments powered by Disqus