Game Development Community

dev|Pro Game Development Curriculum

Disabling auto-lock idle timer in you iPhone game

by Afan Olovcic · 05/12/2009 (7:46 am) · 3 comments

Accelerometer iPhone is a great feature especially for intuitive designs. But there is one simple "problem" if your game uses only accelerometer for gameplay. If auto lock is turned on the phone, the screen will fade after a period of time when touch screen is not used and eventually turn of the screen. I'm sure we've all run into this once or twice and it can be annoying, It turns out the solution lies in the UIApplication interface.

There is a property called idleTimerDisabled in the UIApplication class which defaults to "NO". Here is an excerpt from documentation:
Quote:The default value of this property is NO. When most applications have no touches as user input for a short period, the system puts the device into a “sleep” state where the screen dims. This is done for the purposes of conserving power. However, applications that don't have user input except for the accelerometer—games, for instance—can, by setting this property to YES, disable the “idle timer” to avert system sleep.

To make this work do the following change in the source.

In platformiPhone/iPhoneMain.mm insert the following code in function _iPhoneRunTorqueMain:
void _iPhoneRunTorqueMain( id appID, UIView *Window, UIApplication *app )
{
        //insert this to disable idle timer
	[UIApplication sharedApplication].idleTimerDisabled = YES;
        //insertion end 

	platState.appID = appID;
         //rest of function body

If you want you can bind it to a script variable if you want to toggle it. For sake of example I placed the toggle in platformiPhone/iPhoneWindow.mm in the Platform::initWindow method:
void Platform::initWindow(const Point2I &initialSize, const char *name)
{
      //method code...blah,blah,blah	
      
       bool gIdle = Con::getBoolVariable( "$pref::iPhone::idleTimerDisabled" );
	
	if( gIdle ) {
		[UIApplication sharedApplication].idleTimerDisabled = YES;
	} else  {
		[UIApplication sharedApplication].idleTimerDisabled = NO;
	}

     //rest of method code
}

Special thanks to my colleague Vedad for his great insight in iPhone internals :):)

About the author

www.bootwars.com


#1
05/12/2009 (8:27 am)
*Thumbs up*
#2
05/12/2009 (9:02 am)
Great info Afan! Can't wait to get down to iPhone development myself!
#3
06/02/2009 (11:39 am)
iPhone is great fun!