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.