views:

44

answers:

2

I initiated an async http call to the server in a viewcontroller, but it take long time sometimes.

Can I put a button on it and then abort the running http call when user clicked that button ? and if that could be done, I'd like also implement this function when user exit current view like, navigate back to its parent view.

A: 

Yes, you can abort an NSURLConnection. This question / answer discusses how to do it correctly:

http://stackoverflow.com/questions/2503652/nsurlconnection-still-calls-delegate-after-cancel-method-has-been-called

JosephH
+1  A: 

Assuming you're using an NSURLConnection, just save the connection object in an ivar or property and then call cancel when you want to cancel the connection.

Your button's action should look something like this:

- (IBAction)tapCancelButton:(id)sender {
    [self.connection cancel];
    self.connection = nil;
}
Robot K