UIDocumentInteractionController是OC语言的一个类,但是他并不是一个controller,而是一个继承自NSObject类。
UIDocumentInteractionController
怎么通过使用UIDocumentInteractionController来预览、分享pdf?
一、主要作用:
- 预览类似pdf、doc、ppt等类型文件的类。
- 可以将用户接收到的文件分享到用户手机上的其他App中。
二、使用方法:
1.创建一个UIDocumentInteractionController类的属性:
@property (nonatomic,strong)UIDocumentInteractionController * document;
2.遵循UIDocumentInteractionControllerDelegate
@interface DocumentControllerTest ()<UIDocumentInteractionControllerDelegate>
3.初始化document
把一个ios.pdf文件放到工程中
@implementation DocumentControllerTest
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Document 展示PDF ";
self.view.backgroundColor = [UIColor whiteColor];
// 1.指定要分享的链接
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ios" ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
_documentController = [UIDocumentInteractionController interactionControllerWithURL:url];
_documentController.delegate = self;
// 1 直接打开预览 pdf
[_documentController presentPreviewAnimated:YES]; // 预览文件
// 2 直接显示包含预览的菜单项:分享等
// [_documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES]; // 菜单操作
}
#pragma mark -- UIDocumentInteractionControllerDelegate --
- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(nullable SEL)action
{
// 响应方法
NSLog(@"12 %s", __func__);
return YES;
}
- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller performAction:(nullable SEL)action
{
//
NSLog(@"13 %s", __func__);
return YES;
}
- (void)documentInteractionControllerWillPresentOptionsMenu:(UIDocumentInteractionController *)controller
{
// 页面显示后响应
NSLog(@"9 %s", __func__);
}
- (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
{
// 取消时响应
NSLog(@"10 %s", __func__);
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
NSLog(@"1 %s", __func__);
return self;
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
NSLog(@"2 %s", __func__);
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
{
NSLog(@"3 %s", __func__);
return self.view.frame;
}
// 文件分享面板退出时调用
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller
{
NSLog(@"4 %s", __func__);
NSLog(@"dismiss");
}
// 文件分享面板弹出的时候调用
- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller
{
NSLog(@"5 %s", __func__);
NSLog(@"WillPresentOpenInMenu");
}
// 当选择一个文件分享App的时候调用
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application
{
NSLog(@"6 %s", __func__);
NSLog(@"begin send : %@", application);
}
// Preview presented/dismissed on document. Use to set up any HI underneath.
- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller
{
NSLog(@"7 %s", __func__);
}
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
// 完成时响应
NSLog(@"8 %s", __func__);
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(nullable NSString *)application
{
NSLog(@"11 %s", __func__);
}
三、运行效果
// 1 直接打开预览 pdf,右上角红框的按钮,可以更多操作如:分享、存储、打印等
方式2: // 2 直接显示包含预览的菜单项 (可以选择:“快速查看”开pdf)
问题:
iOS presentOptionsMenuFromRect 点击快速查看 Quick Look没反应
是UIDocumentInteractionController 创建的是局部变量。把它改成全局变量就好了。