English 中文(简体)
How could connectionDidFinishLoading: run if no file is found on server? [duplicate]
原标题:
This question already has an answer here:
Closed 10 years ago.

Possible Duplicate:
Testing use of NSURLConnection with HTTP response error statuses

This is bizarre; I have an async connection like so:

NSString *url=[NSString stringWithFormat:@"http://www.whatever.com/file"];

NSURL *url2=[NSURL URLWithString:url];
NSURLRequest *req=[[NSURLRequest alloc] initWithURL:url2];
NSURLConnection*con=[[NSURLConnection alloc] initWithRequest:req delegate:self];
[req release];

if(con){
    NSMutableData *data=[[NSMutableData alloc] init];
    self.receivedData=data;
    [data release];
}
else {
    UIAlertView*alert=[[UIAlertView alloc] initWithTitle:@"Error!" message:@"Unable to connect to server." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

Then I have a bunch of standard delegate methods:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[receivedData setLength:0];

}

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

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
[connection release];
self.receivedData=nil;

UIAlertView*alert=[[UIAlertView alloc] initWithTitle:@"This app requires Internet" message:[NSString stringWithFormat: @"Connection failed.
 Please exit and check your
Internet access."] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];

[alert show];
[alert release];

}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *payload=[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
self.downloaded=nil;
self.downloaded=payload;
[payload release];
[connection release];
self.receivedData=nil;

NSLog(@"This will display if connectionDidFinishLoading runs.");

}

The NSLog there at the end is running, even if the file is not on the server. Why? I would not expect it to have loaded anything, and instead resulted in an error and an alert view.

These methods seem clear at first, but there must be something going on here that I m not getting about the async connection.

最佳回答

It DID finish loading. The fact that it finished with an HTTP error is beside the point.

The NSHTTPResponse object you got in didReceiveResponse will have a .responseCode property with the 404 in it.

问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签