UIWebView delay in loading local content

前端 未结 2 1557
自闭症患者
自闭症患者 2021-01-12 14:42

I\'m displayed a small amount of local content in a UIWebView, roughly 3 paragraphs of text and one 100x100px image. When the UIWebView is pushed onto to the nav controller,

2条回答
  •  -上瘾入骨i
    2021-01-12 14:57

    Problem Confirmed

    I too see a half-second or more delay with a white screen before my UIWebView content appears. This happens only on the first use of a UIWebView during that run of the app. Successive appearances of UIWebView are nearly instantaneous. So it seems to me and other folk that the delay must be due to WebKit libraries needing to load and initialize.

    Warm-up WebKit

    You cannot eliminate the delay. But you can move that delay to the start of your app, to lessen the annoying effect of "blank screen" to your users. The trick is to load UIWebView with a bogus page off-screen, during your app's launch. Build a minimal HTML5 web page in a hard-coded string. I use a correct and valid HTML5 content to minimize the time taken by UIWebView/WebKit to analyze.

    This technique works noticeably well on real hardware (iPhone 3GS), not just the iOS Simulator.

    In my app delegates didFinishLaunchingWithOptions method, the bottom of the ARC-compliant code looks like this:

    …
    [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 = @"blah

    bogus content

    "; [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;

    This technique seems to reduce most but not quite all of the approximate half-second delay during the user's first appearance of UIWebView on screen. I conclude that most of the delay is due to WebKit warming up, but there must be some overhead related to graphically presenting a UIWebView on-screen. If so, we cannot eliminate that remaining overhead with off-screen work. But nevertheless, this technique eliminates most of the initial delay. So the user's first impression of my app won't be "slow".

提交回复
热议问题