NSURLSessionDownloadTask實現斷點下載,對下載任務進行暫停,取消,恢復


NSURLSessionDownloadTask不能實現離線斷點下載

因為該代理方法是直接將下載結果返回,而不是分步存儲磁盤,因此離線斷點下載可以使用NSURLSessionDataTask

#import "ViewController.h"

@interface ViewController ()<NSURLSessionDownloadDelegate>

@property(nonatomic,strong) NSURLSessionDownloadTask *downloadTask;
@property(nonatomic,strong) NSURLSession *session;
@property(nonatomic,strong) NSData  *fileInfoData;//取消下載時保存的數據

@end

@implementation ViewController

-(NSURLSession *)session
{
    if (_session == nil) {
        _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    }
    return _session;
}

-(NSURLSessionDownloadTask *)downloadTask
{
    if (_downloadTask == nil) {
        
        NSURL *url = [NSURL URLWithString:@"http://www.ytmp3.cn/down/59224.mp3"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        _downloadTask = [self.session downloadTaskWithRequest:request];
    }
    return _downloadTask;
}

- (IBAction)startBtnClick:(UIButton *)sender {
    //開始
    NSLog(@"開始下載");
    [self.downloadTask resume];
    
}
- (IBAction)suspendBtnclick:(UIButton *)sender {
    //暫停
    [self.downloadTask suspend];
    NSLog(@"暫停下載");
}
- (IBAction)cancelBtnClick:(UIButton *)sender {
    
    //取消 , [self.downloadTask cancel]普通的取消是不能恢復的.
    //resumeData 可以用來恢復下載的數據
    [self.downloadTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) {
        self.fileInfoData = resumeData;
    }];
    self.downloadTask = nil;//取消后 將downloadTask置空。
}
- (IBAction)resumeBtnClick:(UIButton *)sender {
    //恢復下載
    //如果已經取消了下載,需要拿到可恢復的數據重新創建下載任務
    if (self.fileInfoData.length > 0) {
        //根據可恢復數據重新創建一個新的downloadTask指針指向全局下載任務
        self.downloadTask = [self.session downloadTaskWithResumeData:self.fileInfoData];
        [_downloadTask resume];
        
    }else {
        [self.downloadTask resume];
    }
}

#pragma mark NSURLSessionDownloadDelegate
//bytesWritten 本次寫入數據的大小
//totalBytesWritten 寫入數據的總大小
//totalBytesExpectedToWrite 文件的總大小
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
    //下載進度
    NSLog(@"----%f", 1.0 * totalBytesWritten/totalBytesExpectedToWrite);
}

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{//下載完成調用
    
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *fullPath = [cachePath stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
    //將文件轉移到安全的地方去
    [[NSFileManager defaultManager]moveItemAtURL:location toURL:[NSURL fileURLWithPath:fullPath] error:nil];
    
    NSLog(@"----%@",fullPath);
}

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{//整個請求完成或者請求失敗調用
    
    NSLog(@"didCompleteWithError");
}
@end

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM