【发布时间】:2013-01-31 01:34:07
【问题描述】:
我正在一个应用程序中显示一个 pdf 文件。我想在导航栏上显示“打开方式”选项,显示 iPhone 上安装的可以打开相同 pdf 的应用程序,如果用户选择任何应用程序(例如 pdf 查看器),则 pdf 应该使用 pdf 查看器应用程序打开。
我该怎么做?
请帮忙
提前致谢。
【问题讨论】:
标签: iphone ios objective-c pdf ios-simulator
我正在一个应用程序中显示一个 pdf 文件。我想在导航栏上显示“打开方式”选项,显示 iPhone 上安装的可以打开相同 pdf 的应用程序,如果用户选择任何应用程序(例如 pdf 查看器),则 pdf 应该使用 pdf 查看器应用程序打开。
我该怎么做?
请帮忙
提前致谢。
【问题讨论】:
标签: iphone ios objective-c pdf ios-simulator
要在设备上的可用应用程序中打开文件,请使用 UIDocumentInteractionController 类。
文档交互控制器与委托对象一起为管理用户与本地系统中的文件的交互提供应用内支持。例如,电子邮件程序可能使用此类来允许用户预览附件并在其他应用程序中打开它们。使用此类为预览、打开、复制或打印指定文件提供适当的用户界面。
如果您遇到困难,在 SO 上会有很多 问题。 search results for UIDocumentInteractionController
【讨论】:
此代码将显示您正在寻找的 OpenIn 交互。它适用于 iPhone 和 iPad。在 iPad 上,它位于弹出窗口中。我正在生成 PDF,但如果您只使用一个,则不需要 writeToFile,只需提交 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];
}
【讨论】:
makePDF 和shareButton?
您可以使用“UIDocumentInteractionController”的Apple Default api进行检查。
以下是网址:
希望它能解决您的问题。
干杯。
【讨论】: