AFURLSessionManager 上传下载使用代码说明

 更新时间:2017年09月03日 09:52:02   作者:鸿鹄当高远  
本文通过代码给大家介绍了AFURLSessionManager 上传下载使用说明,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧

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 上传下载使用代码说明,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

  • Android实现简单动态搜索功能

    Android实现简单动态搜索功能

    这篇文章主要为大家详细介绍了Android实现简单动态搜索功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Android 游戏开发入门简单示例

    Android 游戏开发入门简单示例

    本文主要介绍Android 游戏开发,这里整理了详细的开发游戏的资料,并提供简单的游戏示例代码,有兴趣的同学可以参考下
    2016-08-08
  • Android数据库中事务操作方法之银行转账示例

    Android数据库中事务操作方法之银行转账示例

    这篇文章主要介绍了Android数据库中事务操作方法之银行转账,以具体的银行转账为例分析了Android数据库操作中事务的使用与回滚相关操作技巧,需要的朋友可以参考下
    2017-08-08
  • 关于OkHttp中response.body().string()的用法解析

    关于OkHttp中response.body().string()的用法解析

    这篇文章主要介绍了关于OkHttp中response.body().string()的用法解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • Flutter异步操作实现流程详解

    Flutter异步操作实现流程详解

    在Flutter中,借助 FutureBuilder 组件和 StreamBuilder 组件,可以非常方便地完成异步操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-09-09
  • android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍

    android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍

    PopupWindow可以实现浮层效果,主要方法有:可以自定义view,通过LayoutInflator方法;可以出现和退出时显示动画;可以指定显示位置等感兴趣的朋友可以了解下哦,希望本文对你学习android菜单相关开发有所帮助
    2013-01-01
  • Android 添加TextView删除线(代码简单)

    Android 添加TextView删除线(代码简单)

    最近接了个项目,其中有项目需求是这样的,有这么个需求,就是一个产品下有两个价格,一个是市场价,一个是销售价,这时要把市场价添加个删除线;怎么实现呢?下面小编给大家分享一段简单的代码实现Android 添加TextView删除线
    2016-02-02
  • Android仿网易一元夺宝客户端下拉加载动画效果(一)

    Android仿网易一元夺宝客户端下拉加载动画效果(一)

    本文通过一个demo给大家介绍了android仿网易一元夺宝客户端下拉加载动画效果,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • Android的activity学习笔记

    Android的activity学习笔记

    这篇文章主要整理了Android的activity学习笔记,总共有八大亮点,推荐给大家,需要的朋友可以参考下
    2015-09-09
  • android开发教程之handle实现多线程和异步处理

    android开发教程之handle实现多线程和异步处理

    这篇文章主要介绍了android的handle实现多线程和异步处理的示例,大家参考使用吧
    2014-01-01

最新评论