UIDocumentInteractionController

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

UIActivityViewController 是 iOS 中一个非常方便的系统原生分享组件,可以让用户快速、简单地分享内容到短信、邮件、社交平台等。 使用 UIActivityViewController 非常简单,只需要以下几步: 1. 创建分享内容 创建一个 UIActivityViewController 需要先准备好分享的内容。比如,如果要分享一张图片,则需要先将图片转换为 NSData 类型。 ``` let image = UIImage(named: "example") let imageData = UIImageJPEGRepresentation(image!, 0.5) ``` 2. 创建 UIActivityViewController 创建 UIActivityViewController 并将分享内容传入,同时也可以设置一些属性,比如分享时显示的标题、分享完成时的回调等。 ``` let activityViewController = UIActivityViewController(activityItems: [imageData!], applicationActivities: nil) activityViewController.title = "分享图片" activityViewController.completionWithItemsHandler = {(activityType, completed, returnedItems, error) in // 分享完成后的回调 } ``` 3. 显示 UIActivityViewController 最后,将 UIActivityViewController 显示出来即可。 ``` present(activityViewController, animated: true, completion: nil) ``` 这样,就可以让用户方便地分享内容了。UIActivityViewController 支持的分享平台非常多,包括但不限于短信、邮件、社交平台、打印、复制等。 需要注意的是,如果要分享的内容比较大,比如视频文件,建议使用 UIDocumentInteractionController 来实现分享功能,否则可能会出现内存问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值