欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

AFURLSessionManager 上传下载使用代码说明

程序员文章站 2023-12-15 15:22:28
1、下载 creating a download task nsurlsessionconfiguration *configuration = [nsurl...

1、下载 creating a download task

  nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration defaultsessionconfiguration]; 
  afurlsessionmanager *manager = [[afurlsessionmanager alloc] initwithsessionconfiguration:configuration]; 
  nsurl *url = [nsurl urlwithstring:@"http://example.com/download.zip"]; 
  nsurlrequest *request = [nsurlrequest requestwithurl:url]; 
  nsurlsessiondownloadtask *downloadtask = [manager downloadtaskwithrequest:request progress:nil destination:^nsurl *(nsurl *targetpath, nsurlresponse *response) { 
    nsurl *documentsdirectoryurl = [[nsfilemanager defaultmanager] urlfordirectory:nsdocumentdirectory indomain:nsuserdomainmask appropriateforurl:nil create:no error:nil]; 
    return [documentsdirectoryurl urlbyappendingpathcomponent:[response suggestedfilename]]; 
  } completionhandler:^(nsurlresponse *response, nsurl *filepath, nserror *error) { 
    nslog(@"file downloaded to: %@", filepath); 
  }]; 
  [downloadtask resume]; 

2、上传 creating an upload task

 nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration defaultsessionconfiguration]; 
  afurlsessionmanager *manager = [[afurlsessionmanager alloc] initwithsessionconfiguration:configuration]; 
  nsurl *url = [nsurl urlwithstring:@"http://example.com/upload"]; 
  nsurlrequest *request = [nsurlrequest requestwithurl:url]; 
  nsurl *filepath = [nsurl fileurlwithpath:@"file://path/to/image.png"]; 
  nsurlsessionuploadtask *uploadtask = [manager uploadtaskwithrequest:request fromfile:filepath progress:nil completionhandler:^(nsurlresponse *response, id responseobject, nserror *error) { 
    if (error) { 
      nslog(@"error: %@", error); 
    } else { 
      nslog(@"success: %@ %@", response, responseobject); 
    } 
  }]; 
  [uploadtask resume]; 

3、批量上传 creating an upload task for a multi-part request, with progress

nsmutableurlrequest *request = [[afhttprequestserializer serializer] multipartformrequestwithmethod:@"post" urlstring:@"http://example.com/upload" parameters:nil constructingbodywithblock:^(id<afmultipartformdata> formdata) { 
      [formdata appendpartwithfileurl:[nsurl fileurlwithpath:@"file://path/to/image.jpg"] name:@"file" filename:@"filename.jpg" mimetype:@"image/jpeg" error:nil]; 
    } error:nil]; 
  afurlsessionmanager *manager = [[afurlsessionmanager alloc] initwithsessionconfiguration:[nsurlsessionconfiguration defaultsessionconfiguration]]; 
  nsurlsessionuploadtask *uploadtask; 
  uploadtask = [manager 
         uploadtaskwithstreamedrequest:request 
         progress:^(nsprogress * _nonnull uploadprogress) { 
           // this is not called back on the main queue. 
           // you are responsible for dispatching to the main queue for ui updates 
           dispatch_async(dispatch_get_main_queue(), ^{ 
             //update the progress view 
             [progressview setprogress:uploadprogress.fractioncompleted]; 
           }); 
         } 
         completionhandler:^(nsurlresponse * _nonnull response, id _nullable responseobject, nserror * _nullable error) { 
           if (error) { 
             nslog(@"error: %@", error); 
           } else { 
             nslog(@"%@ %@", response, responseobject); 
           } 
         }]; 
  [uploadtask resume]; 

4、数据任务 creating a data task

  nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration defaultsessionconfiguration]; 
  afurlsessionmanager *manager = [[afurlsessionmanager alloc] initwithsessionconfiguration:configuration]; 
  nsurl *url = [nsurl urlwithstring:@"http://httpbin.org/get"]; 
  nsurlrequest *request = [nsurlrequest requestwithurl:url]; 
  nsurlsessiondatatask *datatask = [manager datataskwithrequest:request completionhandler:^(nsurlresponse *response, id responseobject, nserror *error) { 
    if (error) { 
      nslog(@"error: %@", error); 
    } else { 
      nslog(@"%@ %@", response, responseobject); 
    } 
  }]; 
  [datatask resume]; 

5、请求参数设置 request serialization

request serializers create requests from url strings, encoding parameters as either a query string or http body.
  nsstring *urlstring = @"http://example.com"; 
  nsdictionary *parameters = @{@"foo": @"bar", @"baz": @[@1, @2, @3]}; 

总结

以上所述是小编给大家介绍的afurlsessionmanager 上传下载使用代码说明,希望对大家有所帮助

上一篇:

下一篇: