Nikhil J Joshi
Nikhil J Joshi

Reputation: 1197

reloading UIWebView makes it blank

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

Upvotes: 1

Views: 1306

Answers (3)

Vladimir Vlasov
Vladimir Vlasov

Reputation: 2090

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.

Upvotes: 0

Nikhil J Joshi
Nikhil J Joshi

Reputation: 1197

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

Upvotes: 1

matt
matt

Reputation: 534893

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.

Upvotes: 1

Related Questions