UIWebView VS WKWebView to load local html

后端 未结 4 971
梦如初夏
梦如初夏 2020-12-11 23:14

i create a html string with 500 p tag with timestamp in it

i use UIWebView and WKWebView loadHTMLString:baseURL: to load it,and wkWeb

相关标签:
4条回答
  • 2020-12-11 23:27

    WKWebView is faster for displaying html from Strings. However, there is a bug that makes UIWebView faster by default, which is the phone number detection.

    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.

    webView.loadHTMLString(HtmlStringInstance, baseURL: nil)
    

    I'm by far not the only one to find this:

    • when I use wkwebview to load local html,I find it renders slower than uiwebview
    • UIWebView delay in loading local content
    • Slow loading UIWebView from string
    • Delay in loading a HTML string into a UIWebView
    • Why UIWebView work so slowly when loadHTMLString with UIWebView?

    However, the solution is easy: Disable phone number detection for WKWebView and poof. There you go.

    0 讨论(0)
  • 2020-12-11 23:44

    For me, creating static variable to avoid creating WKWebView multiple times worked. Objective-C example:

    - (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;
    }
    
    0 讨论(0)
  • 2020-12-11 23:49

    WKWebView rendering performance is noticeable in WebGL games and something that runs complex JavaScript algorithms which UIWebView lacks.

    You check the performance both in the Github.

    0 讨论(0)
  • 2020-12-11 23:49

    To make WKWebView faster, disabling WKWebView's data detectors worked for me. 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:

    config.dataDetectorTypes = [.address]
    
    0 讨论(0)
提交回复
热议问题