if (status != 200) [self cancel:connection]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { /////// to do your logic } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [self cancel:connection]; } - (void)cancel:(NSURLConnection *)connection { [connection cancel]; /////////to do your logic. } 看苹果文档的解释 After this method is called, the connection makes no further delegate method calls. If you want to reattempt the connection, you should create a new connection object.
2.调用cancel会回调didFailWithError,前提是你不是在delegate回调里调用的cancel。如didReceiveData这个回调方法,你如果在这个方法里调用cancel的话,则不会调用didFailWithError
2. 发送cancel消息不会调用任何的代理的方法。看一下下面的伪代码
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSInteger status = [(NSHTTPURLResponse *)response statusCode];
if (status != 200)
[self cancel:connection];
} - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
/////// to do your logic
} - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[self cancel:connection];
}
- (void)cancel:(NSURLConnection *)connection {
[connection cancel];
/////////to do your logic.
}
看苹果文档的解释
After this method is called, the connection makes no further delegate method calls. If you want to reattempt the connection, you should create a new connection object.