Reputation: 23
I would like to share/open the PDF files in my app in another application.
The two main apps I would like to share with are Dropbox & PDF Expert.
This is the code I was using but it is not working. For example, if I try to share the file via airdrop, email and WhatsApp it doesn't share anything
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf", @"test"]];
NSURL *URL = [NSURL fileURLWithPath:fullPath];
NSArray *activityItems = [NSArray arrayWithObjects:URL, nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
test.pdf file is in my project.
If there is a way to share PDFs from a server? It would be better so I would not need to download the file in my app and then share it.
Upvotes: 0
Views: 1341
Reputation: 797
Try this.
// In your header. MyViewController.h
@interface MyViewController : UIViewController
<UIDocumentInteractionControllerDelegate>
{
UIDocumentInteractionController *docController;
}
// In your implementation. MyViewController.m Open Results is hooked up
to a button.
- (void)openResults
{
// Generate PDF Data.
NSData *pdfData = [self makePDF];
// Create a filePath for the pdf.
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory
stringByAppendingPathComponent:@"Report.pdf"];
// Save the PDF. UIDocumentInteractionController has to use a physical
PDF, not just the data.
[pdfData writeToFile:filePath atomically:YES];
// Open the controller.
docController = [UIDocumentInteractionController
interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
docController.delegate = self;
docController.UTI = @"com.adobe.pdf";
[docController presentOpenInMenuFromBarButtonItem:shareButton
animated:YES];
}
Upvotes: 1