Reused UIWebView showing previous loaded content for a brief second on iPhone

风格不统一 提交于 2019-11-28 20:52:22

Thanks malaki1974, in my case I wasn't using a modal view. When I sat with an Apple engineer on WWDC 2010 and asked him this question his answer was simply: "Don't reuse UIWebViews, that's not how they were ment to be used." Since then I make sure to calls this set of lines before allocating a new UIWebView

[self.myWebView removeFromSuperview];
self.myWebView.delegate = nil;
[self.myWebView stopLoading];
[self.myWebView release];

That solved the issue.

Clear the contents of the webview before you try to load new content

[self loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];

First, the UIWebView renders it contents in a background thread. Even when you receive webViewDidFinishLoad: it might not be completely done. Specially if it is an ajax-intense page that comes from the network.

You say you are hiding the view. I wonder if that means that the webview delays its drawing completely. What you could try is to move the UIWebView offscreen or obscure it with another view. Maybe that will change it's drawing behaviour.

If you do not need an interactive UIWebView then you can also consider to do it completely offscreen in a separate UIWindow and then create an image from that UIWebView's layer.

That's what I do, and it works:

[_webView stringByEvaluatingJavaScriptFromString:@"document.open();document.close();"];

Try loading a local file that is blank or has a loading graphic when you hide it, rather than just loading new content when you show it. Since the file is local it will be quick and even if the new page takes a while to load it will have either blank or loading expected behavior.

malaki1974

If you got controll over the html. You can communicate back to objective-c when the document is ready. Like so in jQuery:

function messageNative (name, string) {

    var iframe = document.createElement("IFRAME");

    iframe.setAttribute("src", "appscheme://" + name + "/" + string);
    document.documentElement.appendChild(iframe);
    iframe.parentNode.removeChild(iframe);
    iframe = null;
} 

$(function() {  
    messageNative('webview', 'ready');
});

And then in UIWebView's delegate method webView:shouldStartLoadWithRequest:navigationType: wait for the request with url equal to "appscheme://webview/ready". Then you should know: the document is loaded and ready for display. Then all that is missing is a simple fade-in or something like that :)

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