2

I know many topics already exist but none of them seems to enlighten my problem.

I am supposed to get a PDF file back from a JSON request on a server. The downloading task process is called but when I try to store my NSData to a file, it does not succeed but does not return any error.

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {

    NSData *dataPdf = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://..."]];

    /// Get path directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil];

    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"msc.pdf"];
    NSLog(@"filePath: %@", filePath); /Users/.../Devices/.../Documents/msc.pdf

    NSError *error = nil;
    BOOL success = [dataPdf writeToFile:filePath options:NSDataWritingAtomic error:&error];
    if (error) {
        NSLog(@"Error:\n%@", error.localizedDescription); // -> Not called
    }
    else {
        NSLog(@"Success? %hhd", success); // -> Returns NO
    }
}

The same piece of code works for an other PDF download (but this one is more complicated since it involves some script on the server-side and not a direct link).

Does anyone have an idea what is going wrong?

I don't know if it could make things more clear but I don't know exactly if I really get the PDF file I am supposed to get. All that I know is that there are at least some bytes downloaded (NSURLSession didWriteData returns 487) that should be possible to write on the device since NSData is not empty/nil.

Trichophyton
  • 465
  • 3
  • 19
  • 1
    Not sure, but one detail: You should not be checking whether `error` is nil, you should check whether `success` is `YES` or `NO`. Checking the error parameter like this can get incorrect results in some cases. – Tom Harrington Sep 26 '17 at 14:51
  • Yes you are correct but I have done this way to check if there was an error since 'if success' was not called – Trichophyton Sep 27 '17 at 22:57
  • What I’m saying is that this is the wrong way to check for errors, and may lead to your code incorrectly treating success as failure. If you’re doing it this way, you’re doing it wrong, and your code will not work reliably. – Tom Harrington Sep 28 '17 at 00:56
  • Yes I understand but what I want to say is that it results NO (i.e I have no file written) but not because I have an error (since the error condition is not called) meaning that the problem is somewhere else. I totally agree that in real life I have to test "success or not" but in this case it was just to show that I had no error despite not being able to write the file. But I still don't understand why success returns NO. Or maybe I did not understand what you mean. – Trichophyton Sep 28 '17 at 15:49

1 Answers1

0

Make sure dataPdf is not nil - I just made this mistake myself!

Rob Hogan
  • 1,917
  • 15
  • 20