UIWebView white screen on every launch while preloading all tabs

。_饼干妹妹 提交于 2019-12-06 15:31:52

First, have UIWebView on each tab hidden until it has finished loading, then show it. Underneath the UIWebView you can have some placeholder image to describe it loading. Then using the webViewDidFinishLoad delegate method show the web view when it has finished loading. This approach will be non blocking.

- (void)webViewDidFinishLoad:(UIWebView *)webview
{
    if (webview.isLoading)
        return;
    else
        webView.hidden = false;
}

Second, preload the first tab then load the subsequent tabs while displaying the first. You can do that by placing the following code in the first tabs viewDidLoad method:

// Preload the subsquent tabs
for (UIViewController *aVC in self.tabBarController.viewControllers)
    if ([aVC respondsToSelector:@selector(view)] && aVC != self)
        aVC.view;

This way, the additional tabs web views are loaded in the background in a non blocking manner. You could combine it with hiding the web views while loading in case the user navigates to the additional tabs before their pages load.

I tested this with three tabs and it worked nicely.

So the first view controller could look something like this: @implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Load the website
    [self loadWebView];

    // Preload the subsquent tabs
    for (UIViewController *aVC in self.tabBarController.viewControllers)
        if ([aVC respondsToSelector:@selector(view)] && aVC != self)
            aVC.view;
}

-(void)loadWebView
{
    // Create Request
    NSURL *url = [NSURL URLWithString:@"http://anandTech.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // Load the page
    [webView loadRequest:request];
}

- (void)webViewDidFinishLoad:(UIWebView *)webview
{
    if (webview.isLoading)
        return;
    else
        webView.hidden = false;
}

EDIT: Removed GDC as it was crashing when the views had WebViews

Nitish

This post made my day. I am now using NSNotificationCenter for this.

It's hard to identify the where the problem is directly.

First, I suggest that you check your network connection by loading the page on a desktop machine on the same Wifi to see if it is your server that is too slow.

Also you can load your pages one by one instead of load all pages concurrently, since multiple HTTP request are not processed in a FIFO sequence, they may be processed out of order, and your page may need all resource to be loaded before displaying.

Do you control the web page your self? You can use safari inspector to inspect your web page to see where is the time spent, is it resource loading, javascript processing or rendering.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!