AFN3.0前后的断点下载

3.0前用AFHTTPRequestOperation

#pragma mark 下载文件
/**
 下载文件
 
 @param willDownloadFilePath 文件地址
 @param isSupportBreakpointResume 是否支持断点续传
 @param downloadProgressBlock 下载进度
 @param downloadSuccessBlock 成功块
 @param downloadFailureBlock 失败块
 */
- (void)downloadFileAtPath:(NSString *)willDownloadFilePath supportBreakpointResume:(BOOL)isSupportBreakpointResume downloadProgressBlock:(void (^)(float downloadProgress))downloadProgressBlock downloadSuccessBlock:(void (^)(AFHTTPRequestOperation *operation, id responseObject))downloadSuccessBlock downloadFailureBlock:(void (^)(AFHTTPRequestOperation *operation, NSError *error))downloadFailureBlock {
    
    if (!(willDownloadFilePath && willDownloadFilePath.length)) {
        
        NSLog(@"--下载地址--%@--有问题--停止下载--", willDownloadFilePath);
        return;
    }
    NSString *localCacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *localDownloadFilePath = [localCacheDir stringByAppendingPathComponent:[willDownloadFilePath lastPathComponent]];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:willDownloadFilePath]];
    
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    if (!isSupportBreakpointResume) {
        [fileMgr removeItemAtPath:localDownloadFilePath error:nil];
        
        AFHTTPRequestOperation *downloadOp = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        downloadOp.outputStream = [NSOutputStream outputStreamToFileAtPath:localDownloadFilePath append:YES];
        [downloadOp setCompletionBlockWithSuccess:downloadSuccessBlock failure:downloadFailureBlock];
        [downloadOp start];
        
        return;
    }
    
    ///  检查文件是否已经下载了一部分
    unsigned long long downloadedBytes = 0;
    if ([fileMgr fileExistsAtPath:localDownloadFilePath]) {
        //获取已下载的文件长度
        downloadedBytes = [self fileSizeForPath:localDownloadFilePath];
        if (downloadedBytes > 0) {
            NSMutableURLRequest *mutableURLRequest = [request mutableCopy];
            NSString *requestRange = [NSString stringWithFormat:@"bytes=%llu-", downloadedBytes];
            [mutableURLRequest setValue:requestRange forHTTPHeaderField:@"Range"];
            request = mutableURLRequest;
        } else {
            [fileMgr removeItemAtPath:localDownloadFilePath error:nil];
        }
    }
    //不使用缓存,避免断点续传出现问题
    [[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];
    
    AFHTTPRequestOperation *downloadOp = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    downloadOp.outputStream = [NSOutputStream outputStreamToFileAtPath:localDownloadFilePath append:YES];
    if (downloadProgressBlock) {
        
        [downloadOp setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
            
            float downloadProgress = ((float)totalBytesRead + downloadedBytes) / (totalBytesExpectedToRead + downloadedBytes);
            downloadProgressBlock(downloadProgress);
        }];
    }
    [downloadOp setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        NSLog(@"--文件--下载成功--%@--", willDownloadFilePath);
        if (downloadSuccessBlock) {
            downloadSuccessBlock(operation, responseObject);
        }
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        
        NSLog(@"--文件--下载失败--%@--", error.localizedDescription);
        if (downloadFailureBlock) {
            downloadFailureBlock(operation, error);
        }
    }];
    
    // 开启下载
    [downloadOp start];
}


3.0后就没这么简单了,得多写点代码
可参考:
iOS + AFN3.0 断点下载及异常中断处理

你可能感兴趣的:(AFN3.0前后的断点下载)