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.

+3


source to share


3 answers


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];
}

      

+3


source


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

+3


source


You can check using Apple's default api "UIDocumentInteractionController".

Below is the url:

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html

Hope it solves your problem.

Greetings.

+1


source







All Articles