Skip to main content

Loading NSData into a UIWebView



In my web browser, I am trying to load a UIWebView with NSData obtained from an 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


Comments

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

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...