Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 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?

like image 863
Roi Avatar asked Feb 22 '10 14:02

Roi


People also ask

What is a UIWebView iphone?

What is UIWebView? UIWebView is a deprecated iOS user interface control in Apple's UIKit framework. It loads HTML files and web content into an app view, rendering them as they would appear in a browser window.

Is UIWebView deprecated?

Apple is phasing out UIWebView, which is used by developers for integrating web content into an app in a quick and secure manner. Apple is replacing UIWebView (and WebView) with WKWebView, an updated version, as UIWebView has been deprecated.


2 Answers

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.

like image 160
Roi Avatar answered Oct 06 '22 17:10

Roi


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

[self loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
like image 30
hfossli Avatar answered Oct 06 '22 17:10

hfossli