1

I am loading a webpage in a UIWebView using the following method

 [NSURLConnection sendAsynchronousRequest:request
                                    queue:loadingQueue
                        completionHandler:^(NSURLResponse *response,
                                           NSData *data,
                                           NSError *error) {
                            if(!error) {
                                [_webView loadData: data
                                          MIMEType: [response MIMEType]
                                  textEncodingName: [response textEncodingName]
                                           baseURL:nil];
                           }
                       }];

Firstly, for a few pages the images are not loaded at all. And secondly, when I refresh a page later by

      [_webView reload];

The web view turns blank.

Does anyone know what's happening here?

TIA, Nikhil

Nikhil J Joshi
  • 1,177
  • 2
  • 12
  • 25

3 Answers3

1

I am loading a webpage in a UIWebView using the following method [NSURLConnection sendAsynchronousRequest

Well, don't. UIWebView already knows how to fetch the data from the Internet, and loads its request asynchronously. So just hand UIWebView the request by calling loadRequest:. Now sit back and wait for the delegate messages to arrive. If there's a problem, the delegate messages will tell you.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • my problem is, I am loading multiple UIWebViews and I realized NSURLConnection is the only way to do it like multi-threading would have done. Else my UIWebViews load really slowly (as they all populate the main thread). Any suggestion? – Nikhil J Joshi Apr 28 '13 at 21:40
  • No, they do not. I just said, it happens asynchronously. That means that UIWebView does its own background threading. It is already multithreaded. – matt Apr 28 '13 at 21:42
  • you are perhaps right. I was distracted by the multiple reloads/flickers in a view and slower delivery with the loadRequest method of UIWebView. – Nikhil J Joshi Apr 28 '13 at 21:44
  • What is causing the delays and flickering is probably the load on the *network* (multiple simultaneous requests). You can stop the flickering with `suppressesIncrementalRendering` (on iOS 6), but it might be wise to use the delegate methods to load the web views sequentially. – matt Apr 28 '13 at 21:48
  • Alternatively, load the web views before showing them (and use the delegate methods to know when that has happened). – matt Apr 28 '13 at 21:49
  • I was thinking along these lines, unable to guess how one does it with UIWebViews. Could you give me a small snippet of this logic (I meant, first load the UIWebViews completely and then render) – Nikhil J Joshi Apr 28 '13 at 22:08
  • What I couldn't figure out was, due to those independently loaded iFrames, I considered it impossible to guess when the loading is complete, exactly. – Nikhil J Joshi Apr 28 '13 at 22:10
  • Hmm. So you're saying `webViewDidFinishLoad:` arrives but it's lying? – matt Apr 28 '13 at 22:53
  • sort of.... the webViewDidStart ... didFailLoadWithError, and ...didFinishLoad cycles for every new iFrame the webpage may initiate via its own js/jQuery. I keep a counter for this. Though it doesn't seem sufficient to decide whether the content is fully loaded or something will start later. – Nikhil J Joshi Apr 28 '13 at 23:35
  • Didn't know about this. I see that it is discussed here: http://stackoverflow.com/questions/1842370/uiwebview-didfinishloading-fires-multiple-times - This is seems like a major shortcoming in the delegate methods. – matt Apr 28 '13 at 23:50
1

After a few iterations and web searches, I understood that this problem occurs mainly (if not only) because of sites with relative paths.

Expecting that at least the first problem will be solved, I modified the

     [_webView loadData: data
               MIMEType: [response MIMEType]
       textEncodingName: [response textEncodingName]
                baseURL: nil];

to include the baseURL as

                baseURL: [request URL]];

Surprisingly BOTH problems were solved. I can understand why it helped solve the "images not downloading" part, but I am unable to guess the reason for why an already (successfully) loaded page would turn blank, if the baseURL is absent.

Can anyone shed some light on this?

Thanks, Nikhil

Nikhil J Joshi
  • 1,177
  • 2
  • 12
  • 25
0

For me, the first call of the loadData works well, but next calls load blank pages.

I tried the following workaround and it works:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
[webView loadData:data MIMEType:mimeType textEncodingName:@"UTF-8" baseURL:[NSURL new]];

Thus a call of the loadRequest fixes the webView's state.

Vladimir Vlasov
  • 1,860
  • 3
  • 25
  • 38