UIWebView delay when loading local content

UIWebView displays a small amount of local content, approximately 3 paragraphs of text and one 100x100px. When a UIWebView is entered on the nav controller, it takes about half a second to display the content. This happens on the device, not in the simulator. Obviously, a UIWebView needs to do some work before displaying the content, but is there a way to do this before the pushed view appears? I was not lucky to do it myself.

Here I create and click on a view controller containing a UIWebView:

ArticleViewController* viewController = [[ArticleViewController alloc] init];
viewController.article = article;
[viewController view]; //touch the view so it gets loaded
[viewController loadWebView]; //load the web view content
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];

And here is the method loadWebView:

-(void)loadWebView {
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:path];

    NSString* content = article.content;

    NSString *html = [NSString stringWithFormat:@"\
                      <html><body style='background-color: transparent'><style type=""text/css"">body {font-family: helvetica; color: black; font-size: 10pt; margin: 10px 10px 10px 10px}</style>\
                      %@<div style='text-align:center'><a href='13443574_9709e4cf37.jpg?photographer=test' style='-webkit-tap-highlight-color:rgba(0,0,0,0);'><img src='13443574_9709e4cf37.jpg' height='160' width='230'></a></div></body></html>", content];
    [self.webView loadHTMLString:html baseURL:baseURL];
}

I used to have [viewController loadWebView]a viewDidLoad method, but the result seemed to be the same. A blank screen when you click viewController, and then load the contents in half a second.

Any ideas?

+5
2

, : -? "", , -, , -webViewDidFinishLoad: delegate. , .

+1

, UIWebView. UIWebView . UIWebView . , WebKit, .

WebKit

. , " " . , UIWebView . HTML5 - . HTML5, , UIWebView/WebKit .

(iPhone 3GS), iOS-.

didFinishLaunchingWithOptions , ARC- :


[self.window makeKeyAndVisible];

// Performance optimization: Warm up the UIWebView widget and its related WebKit libraries.
// We are choosing the trade-off between (A) a slight delay here during app launch to create and abandon a bogus UIWebView instance, and
// (B) a flash of white and noticeable delay as the UINavigationController slides in from the right the first UIWebView we display during the app run.
UIWebView* bogusWebView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
NSString* html = @"<!doctype html><html lang=en><head><meta charset=utf-8><title>blah</title></head><body><p>bogus content</p></body></html>";
[bogusWebView loadHTMLString:html baseURL:nil]; // Load the page.
bogusWebView = nil; // Not really needed, but self-documents that we intend to discard this object.

return YES;

, -, , UIWebView . , WebKit, , UIWebView . , . , , . , "".

+7

All Articles