2

I'm trying to save PDF file to local storage.

I save the file this way and it seems to me that everything is fine.

//Get path directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

//Create PDF_Documents directory
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"PDF_Documents"];
[[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil];

filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, tastingName];

[tastingNotesData writeToFile:filePath atomically:YES];

This way I try to get the file

tastingPath = /var/mobile/Containers/Data/Application/4255D8B0-33F5-47AA-ABFA-CCC3691DA033/Documents/PDF_Documents/39e0afcdb56240c2a65ab9e136377b32.pdf;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[self.productModel.tastingPath lastPathComponent]];

NSData *data2 = [[NSFileManager defaultManager] contentsAtPath:path];

NSLog(@"tasting notes %@", data2);

At the end file will be displayed in the UIWebView.

What am I doing wrong?

  • [link](https://stackoverflow.com/questions/12707096/how-to-read-pdf-file-from-document-directory-in-iphone) might help – Hooda Aug 15 '17 at 16:24
  • Why do you properly append a path using `stringByAppendingPathComponent` in one place, but then append more to the path using `stringWithFormat`? Do not use `stringWithFormat` to build a path. – rmaddy Aug 15 '17 at 16:30
  • Hooda PDF is saved from API. URL to saved file You have several lines higher, starting with tastingPath – kajz3reczka Aug 15 '17 at 16:31
  • rmaddy I found this solution in another topic, some people wrote that it works perfectly and that's why I used it – kajz3reczka Aug 15 '17 at 16:37
  • @kajz3reczka Just because it happens to work doesn't make it the correct solution. Use `stringByAppendingPathComponent` to build up a path, not `stringWithFormat`. Besides, why be inconsistent and use two different ways to append to a path? Use one (proper) way and be consistent. It makes your code easier to read and maintain and leads to less bugs. – rmaddy Aug 15 '17 at 16:43
  • @rmaddy I will correct the code as you advise – kajz3reczka Aug 15 '17 at 16:48

1 Answers1

2

The problem is simple. In your attempt to read the PDF file, you don't include the PDF_Documents part of the path. Or you are not appending the filename. Can't be 100% sure which part is wrong. It depends on what the value of [self.productModel.tastingPath lastPathComponent] is.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • tastingPath contains the PDF_Document part. [Self.productModel.tastingPath lastPathComponent] is exactly 39e0afcdb56240c2a65ab9e136377b32.pdf – kajz3reczka Aug 15 '17 at 16:40
  • But don't you see the problem? You only access `lastPathComponent` of `tastingPath`. So you are ignoring the `PDF_Document` part of the path. – rmaddy Aug 15 '17 at 16:41
  • holy moly! You have right. Thank you very much :) Now everything work. – kajz3reczka Aug 15 '17 at 16:47
  • Glad to help. Please don't forget to accept answers that solve your issue. It lets people know you question has been answered. – rmaddy Aug 15 '17 at 17:38