How to get download progress in AFNetworking 2.0?

前端 未结 4 1476
一整个雨季
一整个雨季 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:01

    For Download file with progress status use this code

     NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:@"http://..."];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress)
    {
        NSLog(@"Progress: %f", downloadProgress.fractionCompleted);
        if (progressBlock) {
                        progressBlock(downloadProgress);
                    }
    } 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)
    {
        if (response && successBlock) {
                        successBlock(response,filePath);
                    }
        NSLog(@"File downloaded to: %@", filePath);
    }];
    
    [downloadTask resume];
    

提交回复
热议问题