开发者

How to abort async http connection in iphone?

开发者 https://www.devze.com 2023-01-18 07:53 出处:网络
I initiated an async http call to the server in a viewcontroller, but it take long time sometim开发者_开发百科es.

I initiated an async http call to the server in a viewcontroller, but it take long time sometim开发者_开发百科es.

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.


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


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

0

精彩评论

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

关注公众号