IOS开发入门之AFURLSessionManager 上传下载使用
白羽 2018-09-07 来源 :网络 阅读 2069 评论 0

摘要:本文将带你了解IOS开发入门之AFURLSessionManager 上传下载使用,希望本文对大家学IOS有所帮助。

        本文将带你了解IOS开发入门之AFURLSessionManager 上传下载使用,希望本文对大家学IOS有所帮助。


AFURLSessionManager 上传下载使用
  
  
  
   1、下载 Creating a Download Task

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];  
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];  
      
    NSURL *URL = [NSURL URLWithString:@"//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:@"//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:@"//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:@"//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 = @"//example.com";  
    NSDictionary *parameters = @{@"foo": @"bar", @"baz": @[@1, @2, @3]};  

 








   

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!

本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 1
看完这篇文章有何感觉?已经有1人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved