views:

1212

answers:

4

Hello Guys,

i'm developing an app that requires caching web pages (completely) along with their CSS files and images.

so after saving the entire Html of the page ( going through the links to store each file along with the Html file )

later on, 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 beside that file.

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.

A: 

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.

mvds
A: 

Hi,

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:

http://stackoverflow.com/questions/3316881/displaying-a-background-image-on-uiwebview

Robbie
+2  A: 

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.

ACBurk
A: 

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.

Tim van Elsloo