AFNetworking3.0 GET/POST 以及上传(多)文件,下载文件

//初始化AFHTTPSessionManager

- (instancetype)init {

if(self= [superinitWithBaseURL:[NSURLURLWithString:URELHEAD]]) {//请求的链接头

self.securityPolicy= [AFSecurityPolicypolicyWithPinningMode:AFSSLPinningModeNone];

self.requestSerializer= [AFHTTPRequestSerializerserializer];

self.responseSerializer= [AFJSONResponseSerializerserializer];//默认返回的是dictionary

self.requestSerializer.timeoutInterval=30;

}

returnself;

}

//根据请求的类型进行不同的操作

caseTYPE_GET: {

[selfGET:urlparameters:dicprogress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

}];

}

break;

caseTYPE_POST: {

[selfPOST:urlparameters:dicprogress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

[

}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

}];

}

break;

caseTYPE_DOWNLOAD: {//下载文件

NSURLSessionDownloadTask*task

= [selfdownloadTaskWithRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:url]]progress:nildestination:^NSURL*_Nonnull(NSURL*_NonnulltargetPath,NSURLResponse*_Nonnullresponse) {

NSString*path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:response.suggestedFilename];//下载文件的存储目录

return[NSURLfileURLWithPath:path];

}completionHandler:^(NSURLResponse*_Nonnullresponse,NSURL*_NullablefilePath,NSError*_Nullableerror) {

}];

[taskresume];

}

break;

caseTYPE_UPLOAD: {//上传文件

[selfPOST:urlparameters:dicconstructingBodyWithBlock:^(id_NonnullformData) {

if(nil!= fdic) {//单个或多个文件上传

for(NSString*keyin[fdicallKeys]) {

NSString*filePath = [fdicobjectForKey:key];

[formDataappendPartWithFileURL:[NSURLfileURLWithPath:filePath]name:keyerror:nil];

}

}

}progress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

}];

}

break;

default:

break;

你可能感兴趣的:(AFNetworking3.0 GET/POST 以及上传(多)文件,下载文件)