- (NSURLSession *)session {
if (_session == nil) {
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
}
return _session;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSURL *url = [NSURL URLWithString:@"http://localhost/123.mp4"];
NSURLSessionDataTask *task = [self.session dataTaskWithURL:url];
[task resume];
}
#pragma mark - <NSURLSessionTaskDelegate>
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
NSLog(@"完成-->%lu",(unsigned long)task.taskIdentifier);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
NSLog(@"%@", response);
// 對(duì)響應(yīng)做出的處置
/**
NSURLSessionResponseCancel = 0, 取消
NSURLSessionResponseAllow = 1, 允許
NSURLSessionResponseBecomeDownload = 2, 變成下載任務(wù)
*/
completionHandler(NSURLSessionResponseAllow);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
NSLog(@"%tu", data.length);
}
NSURLSession .png