1

I am try to using AFnetworking to download a json file from my server. And I following the official sample code. After download the json, I need to use this json to do something with other local. And I always get nil when I get this json file with my path. Below is my code

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil 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(!error){
        NSLog(@"File downloaded to: %@", filePath);

        if([[NSFileManager defaultManager] fileExistsAtPath:filePath.absoluteString]){
            NSLog(@"file exists");
        } else {
            NSLog(@"file not exists");
        }

    }else{
        NSLog(@"download error %@",error);
    }
}];
[downloadTask resume];

And you see I add a logic to check the file exists or not. Request always success but no file exist. The file path will start with file:///var/... How can I get back this json file?

Jam
  • 11
  • 3
  • Possible duplicate of [NSFileManager.defaultManager().fileExistsAtPath returns false instead of true](https://stackoverflow.com/questions/34135305/nsfilemanager-defaultmanager-fileexistsatpath-returns-false-instead-of-true) – Scriptable May 09 '18 at 13:04

1 Answers1

1

Try using filePath.path instead of filePath.absoluteString when calling fileExistsAtPath

The FileManager needs to be passed a File URL

Scriptable
  • 17,875
  • 4
  • 50
  • 65