+(void)requestWithUrl:(NSString* )url
param:(NSDictionary*)param
completionHandle:(NetworkFetcherCompletionHandle)completion
failureHandle:(NetworkFetcherErrorHandle)failure
{
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
session.requestSerializer.timeoutInterval = 60;// 请求超时时间是60秒
[session.requestSerializer setValue:@”” forHTTPHeaderField:@”lat”];
[session.requestSerializer setValue:@”” forHTTPHeaderField:@”lng”];
if (param != nil) { // 参数不为空的时候
[session POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//DLog(@”data: %@”,responseObject);
if (completion) {
completion(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}else{ // 参数为空的时候
[session POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (completion) {
completion(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
}
10
– (NSURLSessionDataTask *)DELETE:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure;
30
60
所以你只要定义一个变量接收这个对象,然后通过这个对象来放弃请求
NSURLSessionDataTask *req= [session POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
…..blabla
}
然后需要取消的时候直接
[req cancel];
就可以了