Jam
Jam

Reputation: 11

iOS AFNetworking 3.0 file not exist after download file to document path

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?

Upvotes: 1

Views: 287

Answers (1)

Scriptable
Scriptable

Reputation: 19758

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

The FileManager needs to be passed a File URL

Upvotes: 1

Related Questions