Tactics to speed up WKWebView rendering of loadHTMLString-d content?

时光怂恿深爱的人放手 提交于 2019-12-02 20:53:08

An interim answer - I am having some success creating the next view in advance, then using evaluateJavaScript to run document.body.innerHTML = "content" - it does not have the half-second delay. Of course, it means creating a WKWebview earlier than I would otherwise, but hopefully that isn't a performance killer.

Update:

Both WKWebView and UIWebView delays on first run. It is more visible when using WKWebView. I have implemented class which warmups web views to resolve this issue. You can find Swift version of this solution here: https://github.com/bernikovich/WebViewWarmUper

Original answer:

Looks like WKWebView/UIWebView delays on first run. I've created simple class for boosting loading speed of web view with warming up. You can try sample project: https://github.com/bernikovich/NSTViewWarmuper (not available any more)

My way is to go less native and more web. 1. Work out a single page app, like http://m.ftchinese.com/phone.html 2. Bundle every resources (JS, CSS, Graphics as Base64) into one file. 3. Save that file to my Xcode project and use that file to launch the app, setting the baseURL as http://m.ftchinese.com/phone.html

The code here:

let templatepath = NSBundle.mainBundle().pathForResource("index", ofType: "html")!
let base = NSURL(string: "http://m.ftchinese.com")
var s = NSString(contentsOfFile:templatepath, encoding:NSUTF8StringEncoding, error:nil)!
self.webView!.loadHTMLString(s, baseURL:base)

This has some benefits: 1. I've already worked on the web app for many years so it's very stable. 2. The same approach can be used on Android, Windows Phone and Blackberry. 3. SPA has No paging loading. It feels very smooth.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!