开发者

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

开发者 https://www.devze.com 2022-12-20 18:16 出处:网络
In one of my apps I reuse a webview. Each time the user enters a certain view on reload cached data to the webview using the method :-

In one of my apps I reuse a webview. Each time the user enters a certain view on reload cached data to the webview using the method :-

- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL

and I wait for the callback call

- (void) webViewDidFinishLoad:(UIWebView *)webView.

In the mean time I hide the webview and show a 'loading' label. Only when I receive webViewDidFinishLoad do I show the webview.

Many times what happens I see the previous data that was loaded to the webview for a brief second before the new data I loaded kicks in.

I already added a delay of 0.2 seconds before showing the webview but it didn't help.

Instead of solving this by adding more time to 开发者_StackOverflow社区the delay does anyone know how to solve this issue or maybe clear old data from a webview without release and allocating it every time?


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.


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消