(0090)iOS开发之本地文件预览的三种方法(1)

UIDocumentInteractionController是OC语言的一个类,但是他并不是一个controller,而是一个继承自NSObject类。

UIDocumentInteractionController

怎么通过使用UIDocumentInteractionController来预览、分享pdf?

一、主要作用:
  1. 预览类似pdf、doc、ppt等类型文件的类。
  2. 可以将用户接收到的文件分享到用户手机上的其他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 创建的是局部变量。把它改成全局变量就好了。

感谢:
UIDocumentInteractionController对文件的预览,或分享等操作

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值