kajz3reczka
kajz3reczka

Reputation: 43

How to open locally saved PDF file in an Objective C

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?

Upvotes: 2

Views: 1329

Answers (1)

rmaddy
rmaddy

Reputation: 318804

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.

Upvotes: 2

Related Questions