iPhone UIWebView slow loading to local HTML files

Viewed 21097

I'm developing an app that requires caching web pages (completely) along with their CSS files and images after saving the entire HTML of the page (going through the links to store each file along with the HTML file).

While viewing the HTML file offline, UIWebView takes a long time to load the page, given that I'm already offline, and the file is on disk along with its CSS and images.

I'm using this code to load the file:

  NSData *htmlData = [NSData dataWithContentsOfFile:htmlFilePath];
  [wView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:self.htmlFolderPath isDirectory:YES]];

Is there any other means of loading the file to the UIWebView that can load it faster?

P.S: it loads very fast on simulator (offline) but on device it takes a long time (considering its an already offline cached file)

Thanks for help.

7 Answers

I've found certain kinds of CSS can grind WebView rendering to a halt. For example:

body { -webkit-box-shadow:inset 0 0 100px #222; }

This works great in the simulator, looks nice too. But on the phone (even the iPhone 4, iOS 4.2), a simple page will take 10sec to render.

Even in my case, while loading local html files to webview was taking too much time.

Try to load local html files as below, it worked for me:

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"website_and_mobile_tnc-1" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; 
[_TandCView loadHTMLString:[headerString stringByAppendingString:htmlString] baseURL:nil];

If you want to load using NSData, try to make baseUrl to "nil" in your code.

I have modified your code as below,

    NSData *htmlData = [NSData dataWithContentsOfFile:htmlFilePath];
  [wView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];

I didn't tried this, but please try and let me know.

Related