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

后端 未结 6 761
星月不相逢
星月不相逢 2020-12-04 10:32

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 li

相关标签:
6条回答
  • 2020-12-04 11:07

    bfred.it's answer works if you replace the audio-tag with a enter code here -tag - but only if the page is open in iOS10+ Safari AND the user has started the video. You can hide the video with CSS.

    Also, I suspect that this feature will also be removed at some point.

    0 讨论(0)
  • 2020-12-04 11:16

    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.

    0 讨论(0)
  • 2020-12-04 11:16

    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

    0 讨论(0)
  • 2020-12-04 11:20

    You can stop sleeping and screen dimming in iOS Safari by faking a refresh every 20–30 seconds

    var stayAwake = setInterval(function () {
        location.href = location.href; //try refreshing
        window.setTimeout(window.stop, 0); //stop it soon after
    }, 30000);
    

    Please use this code responsibly, don't use it "just because". If it's only needed for a bit, disable it.

    clearInterval(stayAwake); //allow device sleep again when not needed
    

    Tested in Safari iOS 7, 7.1.2, and 8.1, but it may not work in UIWebView browsers like Chrome for iOS or the Facebook app.

    Demo: http://jsbin.com/kozuzuwaya/1

    0 讨论(0)
  • 2020-12-04 11:21

    Even if this approach might not be suitable in every case, you can prevent your phone from locking by reloading the page using Javascript.

    // This will trigger a reload after 30 seconds
    setTimeout(function(){
        self.location = self.location
    }, 30000);
    

    Please note that I tested this with iOS7 beta 3

    0 讨论(0)
  • 2020-12-04 11:31

    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()
    0 讨论(0)
提交回复
热议问题