最近做到一个小功能,需要实现Word、Excel、PDF在线预览,然后百度找到了解决方法,对哦,就是用的UIDocumentInteractionController,说实话需要用到在线预览的地方很少,但是遇到了不会弄那也只有GG了
先来两段瞅瞅
这里要这样
@interface ViewController ()<UIDocumentInteractionControllerDelegate>
@property(nonatomic,strong) UIDocumentInteractionController *documentController;
@end
@implementation ViewController
//直接预览
- (IBAction)pdfAction:(UIButton *)sender {
_documentController = [UIDocumentInteractionController interactionControllerWithURL: [[NSBundle mainBundle] URLForResource:@"Steve" withExtension:@"pdf"]];
_documentController.delegate = self;
//当前APP打开 需实现协议方法才可以完成预览功能
[_documentController presentPreviewAnimated:YES];
}
//使用第三方
- (IBAction)openClick:(UIButton *)sender {
_documentController = [UIDocumentInteractionController interactionControllerWithURL: [[NSBundle mainBundle] URLForResource:@"Steve" withExtension:@"pdf"]];
_documentController.delegate = self;
//第三方打开 手机中安装有可以打开此格式的软件都可以打开/
//[_documentController presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];
//使用AirDrop
[self presentOptionsMenu];
}
-(void)presentOptionsMenu{
[_documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
}
#pragma mark UIDocumentInteractionControllerDelegate
-(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
//注意:此处要求的控制器,必须是它的页面view,已经显示在window之上了
return self;
}
代码很简单,做个笔记以防忘记