AFURLSessionManager 上传下载使用代码说明
1、下载CreatingaDownloadTask
NSURLSessionConfiguration*configuration=[NSURLSessionConfigurationdefaultSessionConfiguration]; AFURLSessionManager*manager=[[AFURLSessionManageralloc]initWithSessionConfiguration:configuration]; NSURL*URL=[NSURLURLWithString:@"http://example.com/download.zip"]; NSURLRequest*request=[NSURLRequestrequestWithURL:URL]; NSURLSessionDownloadTask*downloadTask=[managerdownloadTaskWithRequest:requestprogress:nildestination:^NSURL*(NSURL*targetPath,NSURLResponse*response){ NSURL*documentsDirectoryURL=[[NSFileManagerdefaultManager]URLForDirectory:NSDocumentDirectoryinDomain:NSUserDomainMaskappropriateForURL:nilcreate:NOerror:nil]; return[documentsDirectoryURLURLByAppendingPathComponent:[responsesuggestedFilename]]; }completionHandler:^(NSURLResponse*response,NSURL*filePath,NSError*error){ NSLog(@"Filedownloadedto:%@",filePath); }]; [downloadTaskresume];
2、上传CreatinganUploadTask
NSURLSessionConfiguration*configuration=[NSURLSessionConfigurationdefaultSessionConfiguration]; AFURLSessionManager*manager=[[AFURLSessionManageralloc]initWithSessionConfiguration:configuration]; NSURL*URL=[NSURLURLWithString:@"http://example.com/upload"]; NSURLRequest*request=[NSURLRequestrequestWithURL:URL]; NSURL*filePath=[NSURLfileURLWithPath:@"file://path/to/image.png"]; NSURLSessionUploadTask*uploadTask=[manageruploadTaskWithRequest:requestfromFile:filePathprogress:nilcompletionHandler:^(NSURLResponse*response,idresponseObject,NSError*error){ if(error){ NSLog(@"Error:%@",error); }else{ NSLog(@"Success:%@%@",response,responseObject); } }]; [uploadTaskresume];
3、批量上传CreatinganUploadTaskforaMulti-PartRequest,withProgress
NSMutableURLRequest*request=[[AFHTTPRequestSerializerserializer]multipartFormRequestWithMethod:@"POST"URLString:@"http://example.com/upload"parameters:nilconstructingBodyWithBlock:^(idformData){ [formDataappendPartWithFileURL:[NSURLfileURLWithPath:@"file://path/to/image.jpg"]name:@"file"fileName:@"filename.jpg"mimeType:@"image/jpeg"error:nil]; }error:nil]; AFURLSessionManager*manager=[[AFURLSessionManageralloc]initWithSessionConfiguration:[NSURLSessionConfigurationdefaultSessionConfiguration]]; NSURLSessionUploadTask*uploadTask; uploadTask=[manager uploadTaskWithStreamedRequest:request progress:^(NSProgress*_NonnulluploadProgress){ //Thisisnotcalledbackonthemainqueue. //YouareresponsiblefordispatchingtothemainqueueforUIupdates dispatch_async(dispatch_get_main_queue(),^{ //Updatetheprogressview [progressViewsetProgress:uploadProgress.fractionCompleted]; }); } completionHandler:^(NSURLResponse*_Nonnullresponse,id_NullableresponseObject,NSError*_Nullableerror){ if(error){ NSLog(@"Error:%@",error); }else{ NSLog(@"%@%@",response,responseObject); } }]; [uploadTaskresume];
4、数据任务CreatingaDataTask
NSURLSessionConfiguration*configuration=[NSURLSessionConfigurationdefaultSessionConfiguration]; AFURLSessionManager*manager=[[AFURLSessionManageralloc]initWithSessionConfiguration:configuration]; NSURL*URL=[NSURLURLWithString:@"http://httpbin.org/get"]; NSURLRequest*request=[NSURLRequestrequestWithURL:URL]; NSURLSessionDataTask*dataTask=[managerdataTaskWithRequest:requestcompletionHandler:^(NSURLResponse*response,idresponseObject,NSError*error){ if(error){ NSLog(@"Error:%@",error); }else{ NSLog(@"%@%@",response,responseObject); } }]; [dataTaskresume];
5、请求参数设置RequestSerialization
RequestserializerscreaterequestsfromURLstrings,encodingparametersaseitheraquerystringorHTTPbody. NSString*URLString=@"http://example.com"; NSDictionary*parameters=@{@"foo":@"bar",@"baz":@[@1,@2,@3]};
总结
以上所述是小编给大家介绍的AFURLSessionManager上传下载使用代码说明,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!