ios - 如何在 AFNetworking 3 中通过流式传输下载大文件

标签 ios objective-c iphone afnetworking-3

我想使用 AFNetworking 3 下载大文件。但我需要在出现任何中断(例如互联网丢失或任何其他情况)时恢复下载。如果下载时出现任何中断,我想从提前停止的地方开始下载。是否可以使用 AFNetworking 或者我是否使用任何其他库?请任何人帮助我。 这是我的代码。

NSURLRequest *request = [NSURLRequest requestWithURL:formattedURL];

//Watch the manager to see how much of the file it's downloaded
[manager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
    //Convert totalBytesWritten and totalBytesExpectedToWrite into floats so that percentageCompleted doesn't get rounded to the nearest integer
    CGFloat written = totalBytesWritten;
    CGFloat total = totalBytesExpectedToWrite;
    CGFloat percentageCompleted = written/total;

    //Return the completed progress so we can display it somewhere else in app
    //progressBlock(percentageCompleted);
    NSLog(@"Percentage Completed : %f",percentageCompleted);
    [self updateProgressBar:percentageCompleted];
}];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    //Getting the path of the document directory
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    NSURL *fullURL = [documentsDirectoryURL URLByAppendingPathComponent:@"3511_1464694276.zip"];

    //If we already have a video file saved, remove it from the phone
    return fullURL;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    if (!error) {
        //If there's no error, return the completion block
        //completionBlock(filePath);
    } else {
        //Otherwise return the error block
        //errorBlock(error);
    }

}];

[downloadTask resume];

最佳答案

在这里阅读更多相关信息:https://github.com/AFNetworking/AFNetworking

当您编写了一些代码或自己尝试过一些东西时,请尝试提出问题。

下载文件并显示进度:

- (IBAction)downloadAudio:(id)sender {



    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURL *URL = [NSURL URLWithString:@"http://101songs.com/fileDownload/Songs/0/26958.mp3"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {

        dispatch_async(dispatch_get_main_queue(), ^{
            //Update the progress view
            [_myProgressView setProgress:downloadProgress.fractionCompleted];

        });



    } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        // Do operation after download is complete

    }];
    [downloadTask resume];


}

关于ios - 如何在 AFNetworking 3 中通过流式传输下载大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37698417/

相关文章:

iOS 如何创建类似于 Twitter 心形按钮的动画按钮?

ios - 按钮执行多项操作时出错

iphone - 为什么 CSS 不启动 max-device-width : 480px?

iphone - Unix下针对iphone开发

iphone - 在类之间传递NSString

ios - 如何启用/禁用特定文本字段的 OneTimeCode 功能?

ios - 创建元组数组以调用函数但无法获得返回

objective-c - 如何有条件地将框架包含在 Xcode 项目中?

ios - iOS 8后台获取从未在设备上多次调用

objective-c - 将 ptrace 函数重命名为其他名称?如何?