death_au
death_au

Reputation: 1302

AFNetworking downloaded file missing

I'm using AFNetworking 3.0 to download a file, and it seems to be doing the downloading part fine, but I can't find the file afterwards.

I'm using the code below. In the download task, if I set breakpoints in the destination block, it seems as though the target path and download destination path are correct, and in fact at the point the targetPath points to a tmp file in the tmp folder which exists and contains the correctly downloaded data. However if I then hit a breakpoint in the completion handler block, the tmp file has disappeared and there is no file where my download destination path pointed.

Am I missing a step? Do I have to move this file myself, or is that something AFNetworking should be taking care of?

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

self.theRequest = [[AFHTTPRequestSerializer serializer] 
        requestWithMethod:self.RequestMethod            //@"POST"
                URLString:requestURL.absoluteString     //url to my API
               parameters:self.Parameters               //params being sent to API
                    error:nil];

//headers in this example:
//"Content-Type" = "application/json"
//"X-Requested-With" = XMLHttpRequest
//token = "<API TOKEN>";
for (id key in headers) {
    [self.theRequest setValue:headers[key] forHTTPHeaderField:key];
}

self.theRequest.timeoutInterval = 60 * 100;

NSURLSessionDownloadTask * downloadTask =
        [manager downloadTaskWithRequest:self.theRequest
                                progress:^(NSProgress * _Nonnull downloadProgress) {
                                    if(self.DownloadProgressHandler)
                                    self.DownloadProgressHandler(downloadProgress.fractionCompleted);
                                }
                             destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
                                    NSURL *url = [NSURL URLWithString:self.downloadDestinationPath];
                                    NSLog(@"%@",[targetPath absoluteString]);
                                    NSLog(@"%@",[url absoluteString]);
                                    return url;
                                }
                       completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
                                    [self RequestCompleteWithResponse:response responseObject:[[filePath absoluteString] dataUsingEncoding:NSUTF8StringEncoding] error:error];
                                }];
self.theTask = downloadTask;

[self.theTask resume];

Output from the NSLogs above:

2016-03-04 13:43:44.412 Marq[27505:154492] __23-[MarqAPI BuildRequest]_block_invoke247 line 648 $ file:///Users/aerion/Library/Developer/CoreSimulator/Devices/11594D0A-882C-4E46-9BAC-CEF7148014C7/data/Containers/Data/Application/E8C7D3EE-BB69-461F-BA2F-49EB7C2AE1CF/tmp/CFNetworkDownload_7VGArX.tmp
2016-03-04 13:43:44.425 Marq[27505:154492] __23-[MarqAPI BuildRequest]_block_invoke247 line 649 $ /Users/aerion/Library/Developer/CoreSimulator/Devices/11594D0A-882C-4E46-9BAC-CEF7148014C7/data/Containers/Data/Application/E8C7D3EE-BB69-461F-BA2F-49EB7C2AE1CF/Documents/9dfd86c2-458e-4725-a184-5fcd87f94dbd.inspect

Upvotes: 2

Views: 379

Answers (1)

death_au
death_au

Reputation: 1302

Argh, that was silly of me. The answer is staring me in the face in those logs.
The file path for the temp file begins with file://, whereas my download destination path does not. the answer is to change

NSURL *url = [NSURL URLWithString:self.downloadDestinationPath];

to

NSURL *url = [NSURL fileURLWithPath:self.downloadDestinationPath];

This will give me a valid file path to send the downloaded file to

Upvotes: 4

Related Questions