NSURLSessionDataTask

Posted 王权

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于NSURLSessionDataTask的主要内容,如果未能解决你的问题,请参考以下文章