开发者

using NSURLConnection instead of stringWithContentsOfURL

Guys, I was retrieving an XML response from a .php script on my server using the following code:

 NSString *xmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]
 encoding:NSUTF8StringEncoding error:&error];

and

//tried also: if(error)
if(!xmlString)
{
    NSString * errorString = [NSString stringWithFormat:@"Unable to download xml data (Error code %i )", [error code]];
    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
}

//*** now I have a touchXML xPath query code that extracts what I need from XML

My .php script wasn't working today and my iApp was freezing without any alerts, errors or notifications? I thought that the code above will handle errors but it doesn't???

A) Then what type of error does it catch?

Then I remembered that i I should try working with NSURLConnection and its delegate methods to catch any error that occurs.

//Inside viewDidLoad method        
NSMutableData *responseData = [[NSMutableData alloc] init];

NSURL *baseURL = [[NSURL URLWithString:self.chosenDrawRss] retain];

NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];

[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];

NSString *xmlString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];

The problem I had using this approach was that app ran the Connection in the background and continued executing the rest of the code found in viewDidLoad, so I had to reorganize my code a bit by moving it to another method that I call from delegate method connectionDidFinish开发者_Go百科Loading. The problem I have is that my delegate methods are not called. Well, all except didFailWithError: if I try to load URL that doesn't exist.

UPDATE: delegate methods are called but it takes one minute or so for delegate method to be called and until alert message pops out...

B) Could I use stringWithContentsOfURL and still have an alert to the user if anything happens?

C) If not, then I need help with setting up NSURLConnection approach? I mean, what I'm missing here is why aren't my delegate methods called?

I truly hope my questions make sense :D L


If your delegate method is called in didFailWithError then i suppose it will even be called in connectionDidFinishLoading. Check by enabling a breakpoint in the delegate methods and track the flow. It may have happened the function returns before you call the delegate.

NSString *xmlString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]

should be called in your delegate connectionDidFinishLoading method and in

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

I dont see why the alert should pop up after a minute unless you are doing some processing before displaying it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜