Reputation: 4314
I generated a pdf file in my program and I have it here :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingFormat:@"/CEX.pdf"];
NSData *data = [NSData dataWithContentsOfFile:file];
I know how to save photos in the photo gallery but have no idea what should I do with pdf file and how and where to save it in the device. Can anyone help me please ?! :)
Upvotes: 1
Views: 2704
Reputation: 318944
The code you posted is for reading an existing PDF file from the Documents
directory.
If you want to write the PDF, you need to get the NSData
object representing the PDF, create a path to the file, then use the NSData writeToFile:options:error:
method to save the data to the file.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingPathComponent:@"MyDocument.pdf"];
NSData *pdfData = ... // data representing your created PDF
NSError *error = nil;
if ([pdfData writeToFile:file options:NSDataWritingAtomic error:&error]) {
// file saved
} else {
// error writing file
NSLog(@"Unable to write PDF to %@. Error: %@", file, error);
}
BTW - in your original code, replace:
NSString *file = [documentsDirectory stringByAppendingFormat:@"/CEX.pdf"];
with:
NSString *file = [documentsDirectory stringByAppendingPathComponent:@"CEX.pdf"];
Don't use string formats unless you really have a string format to process.
Upvotes: 3