Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking downloadTaskWithRequest:progress:destination:completionHandler: not writing file to the path

I am trying to download a file using AFNetworking (2.5.4). The download completes, the completion handler is called, with error set to nil, everything seeming fine, but the destination file does not exist:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *fullPath = [valid path from my apps local manager]
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:req progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    return [NSURL URLWithString:fullPath];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    NSLog(@"Saved file to %@.", filePath);
    *** [[NSFileManager defaultManager] fileExistsAtPath:filePath.absoluteString] returns NO here ***
}];
[cell.progressView setProgressWithDownloadProgressOfTask:downloadTask animated:YES];
[downloadTask resume];

The file path is a regular path that my app has write access to:

/var/mobile/Containers/Data/Application/APP-GUID-REDACTED/Documents/FILE-NAME-REDACTED.docx

I was using a different method before AFNetworking, and it could write to the exact same path just fine. HTTP response headers show everything perfectly (status 200, correct content length etc.) and if I curl the download URL it downloads the file with no issues. There's no problem with the file.

Why is my destination file not written in completion handler despite no errors?

UPDATE: I've also tried AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; but it changes nothing. I've also tried creating an NSProgress pointer and sending that for the progress argument, but no avail.

like image 851
Can Poyrazoğlu Avatar asked Jun 16 '15 10:06

Can Poyrazoğlu


1 Answers

Use [NSURL fileURLWithPath:] (not URLWithString).

return [NSURL fileURLWithPath:fullPath];
like image 139
andrei200287 Avatar answered Nov 15 '22 21:11

andrei200287