PreLoad UIWebView on a not yet displayed UIViewController

ios, load, objective-c, uiviewcontroller, uiwebview

Solution

I had your same problem for UIWebView inside a UIPageViewController and I found a better solution.

If you are in `FirstViewController` and you have your webView in your `SecondViewController` put the `loadHTMLString` or `loadRequest` method in the `viewDidLoad` of your `SecondViewController` and call `[secondViewController.view layoutSubviews]` for start loading in your `FirstViewController`.

Ex:

FirstViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    // _secondViewController -> An instance of SecondVieController
    [_secondViewController.view layoutSubviews];
}

SecondViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    NSURL *url =[NSURL URLWithString:@"http://www.google.it"];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

Problem

I have an app where on the very last UIViewController there is a UIWebView... the user navigates through the app and at the very end they can transition (modal) to the final UIViewController with a UIWebView... in it's viewDidLoad I have the UIWebView load the page. The problem is it takes about 8 seconds to load this page and that annoys users. How can I load the UIWebView way ahead of time (like when the app is first launched!) if that UIViewController hasn't even been presented yet?

Original source