gpt4 book ai didi

ios - 屏幕锁定 (IOS) 后,React native Webview 被阻止

转载 作者:行者123 更新时间:2023-12-01 15:28:40 25 4
gpt4 key购买 nike

我正在开发一个 ios react native 应用程序,我正在使用 webview 定期获取数据,并在我的应用程序中使用它,
但是当我锁定我的 iphone 设备时,webview 停止发送数据以响应 native 。

onWebViewLoad() {
setInterval(function(){
this.refs.webview.injectJavaScript('window.ReactNativeWebView.postMessage(window.getData());');
}, 10000);
}

handleMessage(event) {
// receive data
//doSomething(event.nativeEvent.data)
}

<WebView
source={{ uri: this.state.myPage }}
javaScriptEnabled={true}
startInLoadingState={true}
onLoad={this.onWebViewLoad}
ref="webview"
onMessage={this.handleMessage}
/>

注意:当应用程序在前台或后台时代码工作正常,但是当我打开应用程序并锁定屏幕时,webview 停止发送数据。

有没有办法让 webview 在屏幕锁定后发送数据。

最佳答案

它不应该只是被阻止的 WebView。我认为屏幕锁定时计时器也会被阻止。

与名为wake-lock的主题密切相关为此 - 正如文章中提到的 - 有时会使用播放隐藏视频的方法来避免 CPU 休眠。

使用类似的解决方法,您可以尝试此 github post 中的方法在屏幕锁定时使用计时器。 (*)

引自 github post方法的:

...this seems to work for me:

  1. Start background timer in componentWillMount()

  2. Play a silent sound during the time when you want the background timer to be active. This can be something like react-native-sound file which is playing on repeat

  3. Add the following to your info.plist file:

<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>

Note that you should still abide by the apple terms of service, and only do the above if you are in one of their accepted use-cases for background playing.



(*) 请注意,这需要使用 react-native-background-timerreact-native-sound包。它的逻辑和上面的 wake-lock非常相似文章。并且最好仔细考虑您所做的是否符合 IOS 政策。

关于ios - 屏幕锁定 (IOS) 后,React native Webview 被阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59831579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com