9

In my web browser, I am trying to load a UIWebView with NSData obtained from a NSURLConnection. When I try to load it into the UIWebView, instead of the site, it comes up with the HTML plain text.

Here is my code:

in viewDidLoad:

NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://www.msn.com"]];
[NSURLConnection connectionWithRequest: request delegate:self];

later in the code:

 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  {
     webdata = [NSMutableData dataWithData: data];
  }

 -(void)connectionDidFinishLoading:(NSURLConnection *)connection
  {
    [webview loadData:webdata MIMEType: @"text/html" textEncodingName: @"UTF-8" baseURL:nil];
  }

UIWebView loading plain HTML instead of loading the page

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Greg
  • 1,296
  • 2
  • 11
  • 26
  • See https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html – titaniumdecoy Apr 19 '12 at 22:26

1 Answers1

16

You are not appending data that you are receiving. Use this piece of code

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    if (webdata == nil) {
        webdata = [[NSMutableData alloc] init];
    }
    [webdata appendData:data];
}

This method might be called once or more times depending upon your data length. So instead of assigning new data to your ivar, append your data to it so that you have the full response not the last packet of data received.
------------------------------------------------------------------------------------------------------------------------------------
Updated
Or use like this.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
        webdata = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [webdata appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    [mWebView loadData:webdata MIMEType: @"text/html" textEncodingName: @"UTF-8" baseURL:nil];
}
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • 1
    The method above works very well, thanks. Just one thing: it is loading the desktop versions of websites when I use this method as opposed to using UIWebview loadRequest:NSURLRequest. Is there any easy way to load the mobile version of sites by default? (I'm new to this kind of stuff) – Greg Feb 28 '12 at 03:26
  • me too :D .. Put this as a new question. Till then i will find the answer.. and do post here the link of your new question so that if answer come I also let i know how to do it – Inder Kumar Rathore Feb 28 '12 at 03:31
  • Also looking for how to load the mobile version, and use the custom loader to load the clicked links inside the UIWebView – toasted_flakes Dec 11 '12 at 19:46
  • loadData isn't working for me. I just get a blank page. I checked and saw that the HTML code really was in my data. – sudo Oct 10 '13 at 21:35
  • 3
    Thanks, this saved my day! But, `baseURL:nil` is bad: http://stackoverflow.com/q/32763248/1289683 – Dmitry Isaev Sep 24 '15 at 13:56
  • @DmitryIsaev makes sense. – Inder Kumar Rathore Sep 25 '15 at 04:52