Search code examples
iphoneobjective-ciosasihttprequestasiformdatarequest

How to cancel ASIFormDataRequest in iPhone


How to cancel a ASIFormDataRequest using blocks?.

__block ASIFormDataRequest *req = [ASIFormDataRequest requestWithURL:url];  

[req appendPostData:[str dataUsingEncoding:NSUTF8StringEncoding]];
[req setDelegate:self];
[req setCompletionBlock:^{
    [self parseResult:req];
}];
[req setFailedBlock:^{
    [self requestWentWrong:req];
}];
[req setTag:tag];
[req startAsynchronous];

Is there any way to cancel this request in a button action?


Solution

  • Add this code in Your button action Event as req is class memeber variable:

    -(IBAction)YourbuttonactionEvent
    { 
      if(![req isCancelled]) 
      {
        // Cancels an asynchronous request
        [req cancel];
        // Cancels an asynchronous request, clearing all delegates and blocks first
        [req clearDelegatesAndCancel];
    }