Open PDF with other applications
I am showing a pdf file in an attachment. I want to show an "open with" option on the nag bar showing apps installed on the iPhone that can open the same pdf and if the user selects any app (for example to view PDFs) then the pdf should be opened with pdf viewer app.
How to do it?
Please, help
Thanks in advance.
source to share
This code will introduce the OpenIn interaction you are looking for. It works for iPhone and iPad. On iPad, it's in a popover. I'm creating a PDF, but if you just use the one you have, you don't need to write ToFile, just pass the .Path file.
// 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];
}
source to share
To open the file in an accessible application on a device, use the UIDocumentInteractionController class .
The document interaction controller as well as the delegate object provides application support for managing user interactions with files on the local system. For example, an email program can use this class to allow a user to view attachments and open them in other applications. Use this class to represent the appropriate user interface for previewing, opening, copying, or printing the specified file.
It has a lot of questions on it if you get stuck. search results for UIDocumentInteractionController
source to share