ios 防止iOS移动Safari进入空闲/自动锁定/睡眠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9709891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:18:35  来源:igfitidea点击:

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

iphoneiosmobile-safari

提问by forrex

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

在 iOS 应用程序中,您可以设置application.idleTimerDisabled = YES防止手机自动锁定。

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?

我需要在移动 safari 中为像 Doodle Jump 这样的游戏执行此操作,其中用户可能长时间不触摸屏幕。是否有任何记录在案的方法或黑客来做到这一点?

(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.

(更新)他们似乎在这个网站http://www.uncoveryourworld.com 上以某种方式这样做。从您的 iphone 访问,当您到达建筑物/街景并播放背景音乐时,只需留下您的手机即可。它永远不会入睡。

(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:

(更新 2)我花了一些时间仔细研究了他们如何防止手机进入睡眠状态。我做了一个准系统测试,似乎他们在街景中循环音频的方式使它无法入睡。如果您想对此进行测试,只需在您的页面上放置一个简单的音频播放器,然后单击播放:

<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.

我搜索的每个地方都说这是不可能的,所以很高兴看到至少有一些方法可以做到,即使有点黑客。否则,基于浏览器的具有涂鸦跳跃风格的游戏将是不可能的。因此,如果合适,您可以在游戏/应用程序中进行循环,或者只是播放无声循环。

回答by fregante

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

NoSleep.js似乎适用于 iOS 11,据报道它也适用于 Android。



Old answer

旧答案

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

这是一个简单的纯 HTML 方法来做到这一点:循环内嵌自动播放视频(它也可能适用于 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)

在 CodePen 上查看相同的演示(包括秒表)

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 oryou need to start its playback via JS: video.play()
  • 避免为此加载大视频。也许制作一个简短的、微小的、纯黑色的视频或使用
  • 为了使其完全工作,视频需要始终在视口中,或者您需要通过 JS 开始播放:video.play()

回答by robottaxes

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

编辑:这项工作不再有效。目前无法阻止手机在 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.

测试页:点击播放,显示屏将保持亮起,但在某些设备上会变暗,例如装有 iOS 7 的 iPhone。

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

注意:使用这个技巧时要小心,因为它会阻止访问者可能正在使用的任何音乐——而且会惹恼他们。

回答by Emil

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

不,不幸的是,您不能这样做。实现这一点的唯一方法是制作一个 UIWebView 应用程序并设置您在那里提供的变量。
https://stackoverflow.com/a/7477438/267892

回答by Besi

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

即使这种方法可能并不适用于所有情况,您也可以通过使用 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

请注意,我使用 iOS7 beta 3 对此进行了测试

回答by fregante

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

你可以通过每 20-30 秒伪造一次刷新来停止iOS Safari 中的睡眠屏幕变暗

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.

在 Safari iOS 7、7.1.2 和 8.1 中测试,但它可能无法在 UIWebView 浏览器(如 iOS 版 Chrome 或 Facebook 应用程序)中运行。

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

演示:http: //jsbin.com/kozuzuwaya/1

回答by Apeli

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.

如果您用enter code here-tag替换音频标签,则 bfred.it 的答案有效- 但前提是页面在 iOS10+ Safari 中打开并且用户已启动视频。您可以使用 CSS 隐藏视频。

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

此外,我怀疑此功能也将在某个时候被删除。