1

以下代码用于使用以下代码post向服务器发出请求AFHTTPClient

NSURL *url = [NSURL URLWithString:urlString];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        //objects and keys
                        nil];

[httpClient postPath:postPath parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {

   id results = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONWritingPrettyPrinted error:nil];

    //completion block

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@",error);
}];
[httpClient release];

但我注意到,即使用户从当前弹出,请求仍在运行viewController(当记录超时错误时注意到,当在另一个时viewController)。

viewController当不再在视图层次结构中时,如何停止请求。

4

1 回答 1

2

你可以这样做:

[[httpClient operationQueue] cancelAllOperations];

或这个 :

[self cancelAllHTTPOperationsWithMethod:@"POST" path:@"/path"];

您可以将其中一个放入您的viewWillDisappear;

于 2013-09-11T10:13:28.810 回答