使用UIDocumentInteractionController
1.声明一个全局变量
@property (nonatomic, strong) UIDocumentInteractionController *documentController;
2.遵循UIDocumentInteractionControllerDelegate代理
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self;
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
{
return self.view.frame;
}
3.打开的本地文件
_documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
_documentController.delegate = self;// 代理
_documentController.name = _fileName;// 文件名
4.打开预览
直接打开预览界面
[_documentController presentPreviewAnimated:YES];
有查看的文件分享弹窗
[_documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
没有查看的文件分享弹窗
[_documentController presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];