WKWebView在后台运行时不运行JavaScript [英] WKWebView doesn't run JavaScript when on background

查看:203
本文介绍了WKWebView在后台运行时不运行JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到有时Webview移到后台(顶部有另一个View Controller)时,它会停止执行JavaScript函数,并仅在返回到前台时才执行它们.
此处具有类似问题的线程,但是大多数解决方案都是某种形式的黑客攻击,例如将Webview添加到键窗口中,或者循环对评估JavaScript进行虚拟调用,以使Webview不会进入空闲模式并停止JavaScript.
那些解决方案顺便说一句有效,但我一点都不喜欢它们.
我想知道是否有更好的解决方案,是否可以通过配置webView的任何方式来防止进入空闲模式,或者以更优雅的方式对JavaScript进行优先级排序.
谢谢.

I noticed that sometimes when the webview is moved to background (have another view controller on top), it stops JavaScript functions execution, and executes them only when returning back to foreground.
here a thread with a similar issue but most of the solutions was some kind of hack, like adding the webview to the key window, or looping dummy calls to evaluateJavaScript so the webview wouldn't go to idle mode and stop JavaScript.
Those solutions work btw, but I don't like them at all.
I was wondering if there was a better solution for this problem, any way to configure the webView to prevent going into idle mode, or more elegant way to prioritize JavaScript.
Thanks.

推荐答案

不幸的是,WKWebView必须位于视图层次结构中才能使用.您必须已将其添加为屏幕视图控制器的子视图.

Unfortunately, WKWebView must be in the view hierarchy to use it. You must have added it as a sub view of an on-screen view controller.

您可以在屏幕外添加该标签,以使其不可见.隐藏属性可能也起作用.无论哪种方式,都必须调用addSubview使其起作用.

You can add this off-screen so it was not visible. Hidden attribute might have worked as well. Either way you must call addSubview with it to make it work.

其他一些问题并在此处对此进行验证.

所以,我研究了一下,发现了这一点

So, I researched a lit bit a found this

WKWebViewConfiguration* webViewConfig = // set up config

// provide empty frame rect
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero 
configuration:webViewConfig];

// add to keyWindow to ensure it is 'active'
[UIApplication.sharedApplication.keyWindow addSubview:webView];

此方法至少可以确保在应用程序处于活动状态时运行javascript,并且不会影响用户界面.如果以后需要显示它,可以在渲染后从keyWindow中删除webView并重置框架.

This approach at least ensures the javascript runs while the app is active and doesn't effect the UI. If you need to display it later you can remove the webView from the keyWindow after render and reset the frame.

希望这会有所帮助!

编辑

默认情况下,JavaScript是禁用的.要运行Javascript,您必须通过 WKPreferences .您需要将首选项对象的 javascriptEnabled 字段设置为true.

Javascript is disabled by default. In order to run Javascript, you must enable it through WKPreferences. You need to set the javascriptEnabled field of the preferences object to true.

特别是,用于初始化WKWebView的代码应如下所示.

Specifically your code to initialize the WKWebView should look like this.

let contentController = WKUserContentController()
contentController.add(self, name: "handler") 

let configuration = WKWebViewConfiguration()
configuration.userContentController = contentController
configuration.preferences = WKPreferences()
configuration.preferences.javaScriptEnabled = true

webview = WKWebView(frame: self.view.frame, configuration: configuration)
webview.uiDelegate = self
webview.navigationDelegate = self
self.view = self.webview

您还可以查看此帖子以查看其他方法.

Also you can check this post to see a different approach.

这篇关于WKWebView在后台运行时不运行JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆