当前位置:首页 > 资讯 > info5 > 正文

通过NSURLSessionDownloadTask代理实现下载大文件

发表于: 2016-04-17   作者:baohanqing   来源:转载   浏览:
摘要: // //ViewController.m //NSURLSessionDownloadTask // //Createdbyhqon16/4/17. //Copyright©2016年hanqing.Allrightsreserved. // #import"ViewController.h" @interfaceViewController() @property(weak,nonato

通过NSURLSessionDownloadTask代理实现下载大文件_第1张图片


//
//  ViewController.m
//  NSURLSessionDownloadTask
//
//  Created by hq on 16/4/17.
//  Copyright © 2016年 hanqing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <NSURLSessionDownloadDelegate>

@property (weak, nonatomic) IBOutlet UIButton *startBut;

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

- (IBAction)startDownload:(UIButton *)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)startDownload:(UIButton *)sender {
    
    
    NSURLSession *session=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc]init]];
    
    
    NSString *urlString=@"http://120.25.226.186:32812/resources/videos/minion_01.mp4";
    
    NSURLSessionDownloadTask *task=[session downloadTaskWithURL:[NSURL URLWithString:urlString]];
    
    //恢复下载任务
    [task resume];
    
    self.startBut.enabled=NO;
    
}

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
    
    NSLog(@"已下载%lld",totalBytesWritten);
    
    NSLog(@"文件总大小%lld",totalBytesExpectedToWrite);
    
    //self.pro.progress=(1.0*totalBytesWritten/totalBytesExpectedToWrite);
    
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        
        //必须放在主线程当中才回更新进度条-----------------------------
        self.pro.progress=(1.0*totalBytesWritten/totalBytesExpectedToWrite);
    }];

}


-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
    
    //NSLog(@"%@",location);
    
    //下载完成,将文件剪切到我们的cache文件夹当中去
    NSString *cachePath=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    
    NSString *fileName=[cachePath stringByAppendingPathComponent:@"video.mp4"];
    
    NSFileManager *fileManager=[NSFileManager defaultManager];
    
    [fileManager moveItemAtURL:location toURL:[NSURL fileURLWithPath:fileName] error:nil];
    
    NSLog(@"%@",fileName);
    
}


//不管下载成功还是失败,都会来到该方法,不过下载失败的话,error有值,下载成功error为null
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
    
    if (error) {
        
        NSLog(@"下载失败了%@",error);
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end


通过NSURLSessionDownloadTask代理实现下载大文件

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号