iOS本地预览和网络预览PDF等文件(三)——UIDocumentInteractionController本地查看及第三方分享...

项目知识点

区别:

1.UIDocumentInteractionController一次只能浏览一个文件,QLPreviewController可以一起浏览多个文件;2.UIDocumentInteractionController有第三方分享和特定操作,QLPreviewController则没有

UIDocumentInteractionController本地查看文件

1.实例化一个UIDocumentInteractionController对象并遵守其delegate
@interface FileReviewController () <UIDocumentInteractionControllerDelegate>
@property (nonatomic, strong) UIDocumentInteractionController *DIController;
@end
NSURL *urlPath = [[NSBundle mainBundle] URLForResource:@"Jobs" withExtension:@"pdf" subdirectory:@"PDF.bundle"];
self.DIController = [UIDocumentInteractionController interactionControllerWithURL:urlPath];
self.DIController.delegate = self;
2.实现delegate
#pragma mark - UIDocumentInteractionControllerDelegate
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
    return self;
}

- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller {
    return self.view;
}

- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller {
    return self.view.frame;
}
3.点击按钮读取本地文件
- (void)actionDIView {
    [self.DIController presentPreviewAnimated:YES];
}
12879926-1f752d4a76ff97e4.png
效果图1

UIDocumentInteractionController第三方分享

- (void)actionDIThird {
    [self.DIController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
}

在弹出的菜单栏也可以选择Quick Look进行本地查看

12879926-fdcf58ff1f753d51.png
效果图2

第一行列表显示“AirDrop”是苹果在iOS 7提供的一种跨设备分享的技术;
第二行列表展示整个iOS系统中,可以操作PDF文档的应用程序列表,还包括了苹果在iOS 8提供的Share Extension图标;
第三行列表展示现实设备可选的操作,如Copy,Print等;
若没有定义全局变量 UIDocumentInteractionController,此时点击微信等App会报错,原因是 UIDocumentInteractionController过早释放
12879926-abc5161221c2b1e9.png
错误日志1

12879926-da98b9b7c1ebd021.png
错误日志2

12879926-77f2cbeaaea3799f.png
真机效果图

猜你喜欢

转载自blog.csdn.net/weixin_34366546/article/details/86901370