简体   繁体   English

UIWebView VS WKWebView 加载本地html

[英]UIWebView VS WKWebView to load local html

i create a html string with 500 p tag with timestamp in it我创建了一个带有 500 p 标记的 html 字符串,其中包含时间戳

i use UIWebView and WKWebView loadHTMLString:baseURL: to load it,and wkWebView is slower about 50% than UIWebVIew .我使用UIWebViewWKWebView loadHTMLString:baseURL:来加载它,并且wkWebViewUIWebVIew慢约50% why?为什么?

UIWebView:0.1681529879570007
WKWebView:0.3913570046424866

WKWebView is faster for displaying html from Strings. WKWebView 可以更快地显示字符串中的 html。 However, there is a bug that makes UIWebView faster by default, which is the phone number detection.但是,有一个bug让UIWebView默认速度更快,那就是电话号码检测。

Running a viewController with the following code, webView being respectively a UIWebView and WKWebView instance and keeping everything else identical I found WKWebView to take up to 2 seconds to load, while UIWebView loads almost instantly.使用以下代码运行 viewController, webView分别是 UIWebView 和 WKWebView 实例,并保持其他所有内容相同,我发现 WKWebView 最多需要 2 秒才能加载,而 UIWebView 几乎立即加载。

webView.loadHTMLString(HtmlStringInstance, baseURL: nil)

I'm by far not the only one to find this:到目前为止,我不是唯一一个发现这一点的人:

However, the solution is easy: Disable phone number detection for WKWebView and poof.但是,解决方案很简单:禁用 WKWebView 和 poof 的电话号码检测。 There you go.你去吧。

For me, creating static variable to avoid creating WKWebView multiple times worked.对我来说,创建静态变量以避免多次创建 WKWebView 是有效的。 Objective-C example: Objective-C 示例:

- (WKWebView *)webHeaderView
{
    static WKWebView *_webHeaderView = nil;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        if (!_webHeaderView)
        {
            WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
            _webHeaderView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
        }
    });

    return _webHeaderView;
}

WKWebView rendering performance is noticeable in WebGL games and something that runs complex JavaScript algorithms which UIWebView lacks. WKWebView渲染性能在 WebGL 游戏和运行UIWebView缺乏的复杂 JavaScript 算法的东西中很明显。

You check the performance both in the Github .您可以在Github 中检查性能。

To make WKWebView faster, disabling WKWebView's data detectors worked for me.为了使WKWebView更快,禁用 WKWebView 的数据检测器对我有用。 Swift version:迅捷版:

let webViewCofig = WKWebViewConfiguration()
webViewCofig.dataDetectorTypes = []
webView = WKWebView(frame: view.frame, configuration: webViewCofig)

To enable specific data detector, pass the specific type as .address,.link etc while setting dataDetectorTypes:要启用特定数据检测器,请在设置 dataDetectorTypes 时将特定类型作为 .address、.link 等传递:

config.dataDetectorTypes = [.address]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM