UIDocumentInteractionController
UIDocumentInteractionController官方文档介绍如下:
A view controller that previews, opens, or prints files whose file format cannot be handled directly by your app.
一个view controller可以用来预览,打开或者打印,不能被你的app直接处理的文件
使用这个类来预览,打开,复制或者打印特定的文件。例如,一个邮件程序可使用这个类来运行用户来预览附件,和使用其它app来打开附件
也可以使用其代理UIDocumentInteractionControllerDelegate,例如,当文件即将被移交给另一个应用程序打开时,会通知代理
参考文档:
如下的例子,在app中添加一个名为App.pdf
的文件
展示第三方列表
- (IBAction)showList:(id)sender {
NSString *url = [[NSBundle mainBundle] pathForResource:@"App" ofType:@"pdf"];
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:url]];
[self.documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
}
效果如下:
- 第一行展示表示的是AirDrop
- 第二行列表就是整个iOS系统中,可以操作PDF文档的应用程序列表,还包括了苹果在iOS 8提供的
Share Extension
图标 - 第三行列表,就是现实设备可选的操作
例如这里,如果我选择QQ
,会present如下的界面:
直接预览
预览需要实现如下的代理方法:
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller;
这个代理方法主要是用来指定UIDocumentInteractionController要显示的视图所在的父视图容器
使用- (BOOL)presentPreviewAnimated:(BOOL)animated
方法,预览
- (IBAction)preview:(id)sender {
NSString *url = [[NSBundle mainBundle] pathForResource:@"App" ofType:@"pdf"];
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:url]];
self.documentController.delegate = self;
//直接预览
[self.documentController presentPreviewAnimated:YES];
}
#pragma mark - UIDocumentInteractionControllerDelegate
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
效果如下: