How to get download progress in AFNetworking 2.0?

前端 未结 4 1469
一整个雨季
一整个雨季 2020-11-29 02:16

I am using AFURLSessionManager to create a new download task:

AFURLSessionManager* manager = ...

NSProgress* p = nil;
NSURLSessionDownloadTask* downloadTask         


        
4条回答
  •  攒了一身酷
    2020-11-29 03:08

    I faced a similar problem, and found a solution.

    Check the link below: http://cocoadocs.org/docsets/AFNetworking/2.0.1/Categories/UIProgressView+AFNetworking.html

    #import 
    

    and use the additional method available to your UIProgressView

    setProgressWithDownloadProgressOfTask:animated:

    How I did it:

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request  progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response){
        NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) firstObject]];
        return [documentsDirectoryPath URLByAppendingPathComponent:[targetPath lastPathComponent]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error){
        NSLog(@"File downloaded to: %@", filePath);
    
    }];
    
    [self.progressView setProgressWithDownloadProgressOfTask:downloadTask animated:YES];
    [downloadTask resume];
    

提交回复
热议问题