virsir
virsir

Reputation: 15489

How to abort async http connection in iphone?

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.

Upvotes: 3

Views: 1001

Answers (2)

Kris Markel
Kris Markel

Reputation: 12112

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

Upvotes: 2

JosephH
JosephH

Reputation: 37495

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

NSURLConnection still calls delegate AFTER cancel method has been called

Upvotes: 0

Related Questions