views:

84

answers:

2

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];

}
A: 

You are mixing two ways of performing connection - synchronous and asynchronous too. If the asynchronous finishes before the synchronous, you reinitialize webData to empty string. So even if synchronous populated webData it would be cleared.

// you create asynchronous connection which starts downloading in background immediately
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
// you create another synchronous connection which will block until the URL is downloaded
[NSURLConnection sendSynchronousRequest:theRequest returningResponse: &responseHeader error: nil];

// assuming your theRequest is valid, theConnection is always non-nil, so this is always TRUE
if (theConnection) {
     // you reset content of webData, so if your synchronous connection has downloaded something, you throw it away here
     webData = [[NSMutableData data] retain];
} else ...

Try to remove unnecessary/buggy call to sendSynchronousRequest:returningResponse:error: and it might work.

Anyway - where do you populate webData with data ? You did implement connection:didReceiveData: and append what comes into webData, right ? If you didn't - well, that's the problem you are seeing.

Michal
I have populated webData just like you described sorry for not mentioning it from the start. Have updated my question. Seems like some pages can be loaded and other not
ehenrik
Well, I was hoping you did implement that method, but the real problem I descried at the beginning of my answer. Looks like you didn't read carefully, so I added the code with comments to the answer now.
Michal
I have used to get some random crashes that pointed to some mistake with webData since I did the changes that you talked about these crashes have stopped. Thanks a bunch! But however the first problem I had still resist. I have noticed that it is able to load some webpages but some others not. In the case that is not working I have noticed that the NSString returns null connectionDidFinnishLoading, but if i print the data in the didReceiveResponse I get some data, but the last packages arrive as null (differs between 2 - 10 packets).
ehenrik
A: 

I am assuming that webData is an instance variable on your class. I think your problem is that you are not setting webData to the data you are getting from the NSURLConnection. Try something like this:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [webData appendData:data];
}

If that doesn't do the trick try adding an NSLog() after the NSString *html = [[NSString alloc]... line, like so:

NSLog(@"HTML: %@", html);

That will tell you whether you are getting HTML or not and whether the problem is with your NSURLConnection code or your UIWebView IBOutlet.

MattLeff
Sorry for not writing that in my question but i have logged the NSString as you described and I get the source of the webpage. I have succeeded to load some another webpage now with my code but some pages won't load. It looks like it only supports "pure" html like www.pinball.org/rules and not database html like www.pinballhighscores.org/highscores.php?filter_game=medieval+madness
ehenrik
HTML is HTML. It does not matter whether the HTML was a static .html file or whether it was generated from database records. If you know that you have valid HTML, the next thing to check would be the webview. If you try `[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]]`, does it work?
MattLeff
I have noticed that it is able to load some webpages but some others not. In the cases that is not working I have noticed that the NSString returns null in connectionDidFinnishLoading, but if i print the data in the didReceiveResponse I get some data, but the last packages arrive as null (differs between 2 - 10 packets).
ehenrik