UIDocumentInteractionController

本文详细介绍了UIDocumentInteractionController的使用方法,包括如何预览、打开、复制或打印特定文件,以及如何通过代理方法实现更高级的功能。展示了如何在iOS应用中使用此组件来增强文件操作体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UIDocumentInteractionController

UIDocumentInteractionController官方文档介绍如下:

A view controller that previews, opens, or prints files whose file format cannot be handled directly by your app.
一个view controller可以用来预览,打开或者打印,不能被你的app直接处理的文件

使用这个类来预览,打开,复制或者打印特定的文件。例如,一个邮件程序可使用这个类来运行用户来预览附件,和使用其它app来打开附件
也可以使用其代理UIDocumentInteractionControllerDelegate,例如,当文件即将被移交给另一个应用程序打开时,会通知代理

参考文档:

如下的例子,在app中添加一个名为App.pdf的文件

展示第三方列表

- (IBAction)showList:(id)sender {
    NSString *url = [[NSBundle mainBundle] pathForResource:@"App" ofType:@"pdf"];
    self.documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:url]];
    [self.documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
}

效果如下:
01

  • 第一行展示表示的是AirDrop
  • 第二行列表就是整个iOS系统中,可以操作PDF文档的应用程序列表,还包括了苹果在iOS 8提供的Share Extension图标
  • 第三行列表,就是现实设备可选的操作

例如这里,如果我选择QQ,会present如下的界面:
02

直接预览

预览需要实现如下的代理方法:

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller;

这个代理方法主要是用来指定UIDocumentInteractionController要显示的视图所在的父视图容器

使用- (BOOL)presentPreviewAnimated:(BOOL)animated方法,预览

- (IBAction)preview:(id)sender {
    
    NSString *url = [[NSBundle mainBundle] pathForResource:@"App" ofType:@"pdf"];
    self.documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:url]];
    self.documentController.delegate = self;
    //直接预览
    [self.documentController presentPreviewAnimated:YES];
    
}

#pragma mark - UIDocumentInteractionControllerDelegate

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
    
    return self;
    
}

效果如下:

03

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值