Load HTML NSString into a UIWebView

Posted by ehenrik on Stack Overflow See other posts from Stack Overflow or by ehenrik
Published on 2010-10-27T12:56:10Z Indexed on 2012/09/28 3:38 UTC
Read the original article Hit count: 130

Im doing a project where I connect to a webpage using the NSURLConnection to be able to monitor the status codes that are returned (200 OK / 404 ERROR). I would like to send the user to the top url www.domain.com if I recieve 404 as status code and if i recieve as 200 status code I would like to load the page in to a webview. I have seen several implementations of this problem by creating a new request but I feel that it is unnecessary since you already received the html in the first request so i would just like to load that HTML in to the webView.

So i try to use the [webView loadHTMLFromString: baseURL:] but it doesn't always work, I have noticed that when i print the NSString with html in the connectionDidFinnishLoading it sometimes is null and when I monitor these cases by printing the html in didReceiveData a random number of the last packets is NULL (differs between 2-10). It is always the same webpages that doesn't get loaded. If I load them to my webView using [webView loadRequest:myRequest] it always works. My implementation looks like this perhaps someone of you can see what Im doing wrong.

I create my first request with a button click.

-(IBAction)buttonClick:(id)sender
{
    NSURL *url = [NSURL URLWithString:@"http://www.domain.com/page2/apa.html"];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url]
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if( theConnection )
    {
     webData = [[NSMutableData data] retain];
    }
    else
    {
    }
}

Then I monitor the response code in the didReceiveResponse method by casting the request to a NSHTTPURLResponse to be able to access the status codes and then setting a Bool depending on the status code.

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{     
 NSHTTPURLResponse *ne = (NSHTTPURLResponse *)response;
 if ([ne statusCode] == 200){
  ok = TRUE;
 }
 [webData setLength: 0];
}

I then check the bools value in connectionDidFinnishLoading. If I log the html NSString I get the source of the webpage so i know that it isn't an empty string.

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

 NSString *html = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

 NSURL *url = [NSURL URLWithString:@"http://www.domain.com/"];
 if (ok){
  [webView loadHTMLString:html baseURL:url];
  ok = FALSE;
 }
 else{
        //Create a new request to www.domain.com

 }

}

webData is an instance variable and I load it in didReceiveData like this.

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

    [webData appendData:data];

}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uiwebview