Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent iOS mobile safari from going idle / auto-locking / sleeping?

In an iOS app you can set application.idleTimerDisabled = YES to prevent the phone from auto locking.

I need to do this in mobile safari for a game like Doodle Jump where the user may not touch the screen for an extended period of time. Is there any documented method or hack to do this?

(Update) They seem to be doing it somehow in this site http://www.uncoveryourworld.com. Visit from your iphone and when you get to the buildings/street scene with music playing in the background just leave your phone alone. It never goes to sleep.

(Update 2) I've spent some time taking a closer look at how they might be keeping the phone from going to sleep. I've done a barebones test and it seems that the way they are looping the audio in the street scene is what keeps it from going to sleep. If you'd like to test this just put a simple audio player that loops on your page and click play:

<audio src="loop.mp3" onended="this.play();" controls="controls" autobuffer></audio>

Everywhere I searched it is being said that this isn't possible, so it is nice to see there is at least some way to do it even if a bit of a hack. Otherwise a browser based game with doodle-jump style play would not be possible. So you could have a loop in your game/app if appropriate or just play a silent loop.

like image 443
forrex Avatar asked Mar 14 '12 20:03

forrex


3 Answers

NoSleep.js seems to work in iOS 11 and it reportedly works on Android as well.


Old answer

This is a simple HTML-only method to do that: looping inline autoplaying videos (it might also work in Android Chrome 53+)

<video playsinline muted autoplay loop src="https://rawgit.com/bower-media-samples/big-buck-bunny-480p-30s/master/video.mp4" height=60></video>

See the same demo on CodePen (includes a stopwatch)

Notes

  • Avoid loading a big video just for this. Perhaps make a short, tiny, black-only video or use
  • To make it fully work, the videos needs to be always in the viewport or you need to start its playback via JS: video.play()
like image 81
fregante Avatar answered Oct 10 '22 17:10

fregante


Edit: This work around no longer works. It is not currently possible to prevent the phone from sleeping in safari.

Yes, you can prevent the phone to sleep using an audio loop. The trick won't start automatically, you will have to play it when the visitor touches the screen.

<audio loop src="http://www.sousound.com/music/healing/healing_01.mp3"></audio>

Test page: tap play and the display will stay on but it will dim on some devices, like an iPhone with iOS 7.

Note: be careful using this trick because it will stop any music that the visitors might be using—and it will annoy them.

like image 37
BendYourTaxes Avatar answered Oct 10 '22 15:10

BendYourTaxes


No, you can't do this, unfortunately. The only way to achieve this is by making a UIWebView-application and setting the variable you provided there.
https://stackoverflow.com/a/7477438/267892

like image 27
Emil Avatar answered Oct 10 '22 17:10

Emil