Programatically Disable the Idle Timer on iOS using the ObjectiveC Bridge

If you’ve got an iOS device, you’ve probably noticed that if you stop interacting with your device, after a little while it will turn off the screen and go to sleep. This makes perfect sense for devices that are battery challenged at the best of times, but one side effect of this is that whatever app is running will also stop.

For many apps, this is fine, but what if you don’t want your app to stop running? What if, like one project I’m working on currently, it is accepting input from the accelerometer and just because nobody is interacting with it right now, I still want it to record that accelerometer input?

One solution is to disable the device’s idle timer programmatically. Not permanently of course, just while your app is doing whatever it is that needs to keep happening.

The iOS SDK provides an idleTimerDisabled property of the UIApplication class. As far as I can see, this is not directly exposed via FireMonkey anywhere, but this doesn’t stop us. Just like on Windows and OSX, Delphi gives you access to the underlying iOS API.

A TUIApplication factory class, and a UIApplication interface are defined in the iOSapi.UIKit.pas file that ships with Delphi. Referencing TUIApplication.OCClass gives us access to the underlying ObjectiveC class, which we can then use to get hold of the shared UIApplication instance, using the sharedApplication class method.

This returns a raw ObjectiveC pointer, however, so we need to “wrap” this in the UIApplication Delphi interface like so:

Once we have that, we can simply call

to disable sleep mode. Just remember to enable it again when your app is finished doing whatever it is doing, by calling

.

One Comment

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">