Domanda

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?

È stato utile?

Soluzione

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];
}

Altri suggerimenti

What u can do is, load the html string

 NSString *htmlFile = [[NSBundle mainBundle] pathForResource:self.path ofType:@"htm"];
 NSError * error;
 NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:&error];

and when u display the webView load this content in your webView

[self.webView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]];

then your webview need only render your content and doesn't load the data

/// In Swift 2.0

    do{
        let path = NSBundle.mainBundle().pathForResource("theName", ofType: "html")!
        let html = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
        webView.loadHTMLString(html, baseURL: nil)
    }catch{
        print("Error is happend")
    }

Note: It works the same way for WKWebView

Not sure how much work you want to do for this but I think you could cache/store the data from the URL in something like a NSDictionary, or something similar, using [NSUserDefaults standardUserDefaults] and use that information before actually loading the page.

So, load the first time you use it, after it has been seen then store the data using NSUserDefaults. Next time you go to this page, load from the NSUserDefaults first and call the URL/API in the back ground then update NSUserDefaults with this new data.

If you are requesting or pulling specific information each time, then I don't think this will work. If that is your case, you can probably try loading the data/website in an earlier view controller (probably one that takes the user a while to navigate to) and then send that data to the webview. Something like delegate/protocol or maybe even NSNotificationCenter. Hope this helps.

UIWebView is very tricky object. Simple answer would be: no you can't.

UIWebView won't load a document or an URL if it is not in views hierarchy. Don't try doing it in different thread/queue either.

What you can do is to add UIWebView to the views hierarchy at the very beginning (as you've mentioned) and then pass it to the last view controller with preloaded data. This may work, but it's not an elegant way.

Side questions is: why does it take 8 secs to load a page? Maybe you can download the content of this page earlier? Is it static or dynamic?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top