Open pdf with other apps

2019-04-11 11:13发布

I am displaying a pdf file in an app. I want to show "open with" option on nag bar showing apps installed on iPhone that can open same pdf and if user selects any of the app (for e.g. pdf viewer) then the pdf should get open with pdf viewer app.

How do I do this?

Please help

Thanks in advance.

3条回答
虎瘦雄心在
2楼-- · 2019-04-11 11:54

This code will present the OpenIn interaction you're looking for. It works for iPhone and iPad. On iPad its in a popover. I'm generating the PDF but if you're just using one you have you don't need to writeToFile, just hand in the filePath.

// 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楼-- · 2019-04-11 11:58

To open a file in an available application on the device, use the UIDocumentInteractionController class.

A document interaction controller, along with a delegate object, provides in-app support for managing user interactions with files in the local system. For example, an email program might use this class to allow the user to preview attachments and open them in other apps. Use this class to present an appropriate user interface for previewing, opening, copying, or printing a specified file.

There are a lot of questions around it on SO if you get stuck. search results for UIDocumentInteractionController

查看更多
手持菜刀,她持情操
4楼-- · 2019-04-11 12:18

You can check with Apple Default api of "UIDocumentInteractionController".

Below is url:

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

Hope, it will resolve your issue.

Cheers.

查看更多
登录 后发表回答