• NSURLSessionDataTask


    #import "ViewController.h"

    @interface ViewController ()<NSURLSessionDelegate,NSURLSessionTaskDelegate,NSURLSessionDataDelegate>

    @property (weak, nonatomic) IBOutlet UIImageView *imageView;

    @property (weak, nonatomic) IBOutlet UIProgressView *progress;

    @property (nonatomic,strong) NSURLSession * session;

    @property (nonatomic,strong) NSURLSessionDataTask * dataTask;

    @property (nonatomic,strong) NSMutableData * imageData;

    @property (nonatomic,assign) long long length;

    @end

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad]; 

    }

    -(NSURLSession *)session{

        if (_session == nil) {

            NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration];

            self.session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];

        }

        return _session;

    }

    -(NSURLSessionDataTask *)dataTask{

        if (_dataTask ==nil) {

            NSURL * url = [NSURL URLWithString:@"http://img1.3lian.com/2015/a1/124/d/181.jpg"];

            self.dataTask = [self.session dataTaskWithURL:url];

        }

        return _dataTask;

    } 

    - (IBAction)suspendenButtonDidClicked:(UIButton *)sender {

        if (self.dataTask.state == NSURLSessionTaskStateRunning) {

            [self.dataTask suspend];

        }

    }

    - (IBAction)resumeButtonDidClicked:(UIButton *)sender {

        [self.dataTask resume];

    } 

    - (IBAction)cancelButtonDidClicked:(UIButton *)sender {

        if (_dataTask) {

            if (self.dataTask.state == NSURLSessionTaskStateRunning||NSURLSessionTaskStateSuspended) {

                [self.dataTask cancel];

                self.dataTask=nil;

                self.session = nil;

                [self.progress setProgress:0 animated:NO];

            }

        }

    } 

    //获取服务器返回的响应信息

    -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler{

        //转换

        NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;

        NSLog(@"%@",[httpResponse allHeaderFields]);

        //获取资源大小

        self.length = httpResponse.expectedContentLength;

        NSLog(@"responce"); 

        if (_length!=-1) {

            //继续下载或上传

            self.imageData = [NSMutableData dataWithCapacity:(NSUInteger)_length];

            completionHandler(NSURLSessionResponseAllow);

        }else{

            completionHandler(NSURLSessionResponseCancel);

            [self.dataTask cancel];

            self.dataTask = nil;

        }

    }

    //一个数据包过来了

    -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(nonnull NSData *)data{

        NSLog(@"receive data");

        //将当前的数据添加到imageData

        [self.imageData appendData:data];

        float rate = data.length/(_length*1.0);

        [self.progress setProgress:(_progress.progress +rate) animated:NO];

    }

    //下载完毕

    -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{

        NSLog(@"complete");

        if (error) {

            NSLog(@"error");

        }else{

            UIImage * img = [UIImage imageWithData:_imageData];

            self.imageView.image = img;

        }

        self.session = nil;

        self.dataTask = nil;

    }

    @end

  • 相关阅读:
    linux 常见知识2
    python 数组
    linux 常见知识
    python入门1
    linux 入门的常见命令
    聊天机器人開發好消息!!DIALOGFLOW與微信的天作之合!!
    群发短信软件共冶一爐
    Ultimate Facebook Messenger for Business Guide (Feb 2019)
    Top 5 Business Messaging Announcements at Facebook F8 2019
    Ultimate Guide to WhatsApp for Business 2019
  • 原文地址:https://www.cnblogs.com/damonWq/p/5361272.html
Copyright © 2020-2023  润新知