Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've been experimenting with stacking WKWebViews in a UINavigationController as a method of making a hybrid app that's more native than PhoneGap. It broadly works - I hook into decidePolicyForNavigationAction when a WKWebView hits a link, and push a new ViewController with the link it wants.

But the page loading is slow. I've done everything I can think of to speed it up - it's using loadHTMLString rather than a request to ensure everything is local - I've even tried stripping out the CSS and JS to see if that speeds it up, but no dice. It still takes at least 500ms for a short, HTML only, locally stored page to appear in the empty WKWebView. I can tell from debugging that the delay is not in reading the HTML from disk, but the time between loadHTMLString() and didFinishNavigation().

Does anyone have any tactics for fixing this? I'd try to preload the view, only I don't know which link the user is going to tap so I don't know what to preload.

like image 597
Alastair Avatar asked Oct 31 '14 22:10

Alastair


2 Answers

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.

like image 89
Alastair Avatar answered Oct 18 '22 19:10

Alastair


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)

like image 42
Timur Bernikovich Avatar answered Oct 18 '22 19:10

Timur Bernikovich