English 中文(简体)
iPhone UIWebView slow loading to local HTML files
原标题:

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.

问题回答

Is your solution actually cache things other than the html file, meaning pictures, css, etc, AND relinking them in the html page? I am guessing that you are downloading and caching the html page and external resources then the UIWebView is loading that html and still going out to load the external resources. That would explain the fast performance on the simulator and the slower performance on device.

Here is a library designed to do exactly what you are trying to do: ASIWebPageRequest.

It should also be noted that it could simply be a case of disk i/o bottlenecking. I know on my project, I was loading large images in a uitableview and even after they were cached I noticed quite a bit of lag when pulling them off the disk because they were so big.

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.

Since this probably just takes time because it needs to parse and render the page, you may consider firing up the UIWebView in the background; i.e. added as a subview, but not visible.

Maybe the UIWebView is smart enough to know it doesn t need to do anything, but I suspect that at least html and css parsing is done right away.

If it doesn t do anything without being visible, reduce size to 1x1 and set opacity=0, and put that pixel some place where it can t interfere with touch event handling.

Not perfect but it may work.

I m almost sure you will never be able to do this. The UIWebView just needs some time to process your webpage even when it s a local page.

Keeping that in mind you can try to preload the page before it s being shown. For example if you show it after a user presses a button, preload the page when you show the button instead of when the user actually presses the button. The user doesn t notice the slow loading, because it s being handled in the background so when the user presses the button the page is already been loaded.

I can give you an idea about alternative ways of loading HTML from file into the UIWebView. A small project I ve got uses Objective-C as a pure wrapper for UIWebView

The project is here, but the main code is below: http://github.com/robb1e/iWeb

- (void)viewDidLoad {
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html" inDirectory:@"."]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    [super viewDidLoad];
}

I m also exploring ways of improving the perceived performance by showing an image while the DOM is getting ready. Some answers I ve had are here:

Displaying a background image on UIWebView

Just if in case someone happens the same to me...

I had a UIWebView that was loading a string html and every resource (js and css) was stored locally.

I ve faced that loading the content with internet connection it was some kind of slow (1 or 2 seconds to load and appear the webview in my controller) but when I tried to load the same page WITHOUT internet connection it was fast, really fast.

I ve remembered that my HTML template had this in the start <base href="http://somesite.com" /> that I ve used to load some images whit relative paths in some contents.

Removing that work as charm. So that can be making your web view load slower even if you don t have any reference to extern content in your HTML.

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.





相关问题
What to look for in performance analyzer in VS 2008

What to look for in performance analyzer in VS 2008 I am using VS Team system and got the performance wizard and reports going. What benchmarks/process do I use? There is a lot of stuff in the ...

SQL Table Size And Query Performance

We have a number of items coming in from a web service; each item containing an unknown number of properties. We are storing them in a database with the following Schema. Items - ItemID - ...

How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

热门标签