iOS开发之UIDocumentInteractionController
来源:互联网 发布:大淘客cms文章系统 编辑:程序博客网 时间:2024/04/27 17:11
在App的开发过程中,我们避免不了要打开软件中的文件,例如:Excel文件,Word文件,图片文件等不同格式的文件或者想要通过第三方的App来打开这些文件,那么我们就要用到UIDocumentInteractionController和Quick Look来解决这些问题了。
在iOS系统跨App分享内容的几种常用技术,比如 URL Scheme, AirDrop,UIDocumentInteractionController , UIActivityViewController
UIDocumentInteractionController是从iOS 3.2的SDK开始支持的,它是直接继承的 NSObject。
我们就介绍UIDocumentInteractionController的简单使用。
UIDocumentInteractionController
1.展示一个可以操作我们分享的文档类型的第三方App列表
2.在第一条展示列表的基础上添加额外的操作,比如 复制 , 打印 , 预览 , 保存 等。
3.结合 Quick Look 框架直接展示文档内容
核心代码如下:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSURL *url = [[NSBundle mainBundle] URLForResource:@"004" withExtension:@"png"]; // NSString *docu = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; // NSString *filePath = [docu stringByAppendingPathComponent:@"004.png"]; // NSURL *url = [NSURL fileURLWithPath:filePath]; self.document = [UIDocumentInteractionController interactionControllerWithURL:url]; self.document.delegate = self; // 不展示可选操作 // [self.document presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES]; // 展示可选操作 // 可结合代理方法documentInteractionControllerViewControllerForPreview:显示预览 [self.document presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES]; }- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller { return self;}/** * 文件分享面板退出时调用 */- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller { NSLog(@"dismiss");}/** * 文件分享面板弹出的时候调用 */- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller { NSLog(@"WillPresentOpenInMenu"); }/** * 当选择一个文件分享App的时候调用 */- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application { NSLog(@"begin send : %@", application);}效果如下:
注意点有:
1.要实现UIDocumentInteractionController的代理方法必须遵守UIDocumentInteractionControllerDelegate协议。
2.UIDocumentInteractionController属性必须使用retain或strong修饰,控制器必须持有该对象。
3.代码中的两个路径都可以使用。
4.弹出面板的方法一个不展示可选操作,一个展示可选操作。
5.直接使用presentPreviewAnimated:方法弹出预览。
6.结合代理方法documentInteractionControllerViewControllerForPreview:显示预览操作。
直接预览文件还有一个方式:QuickLook,使用方式如下:
1.向项目导入QuickLook.framework。
2.在需要的地方包含头文件#import <QuickLook/QuickLook.h>
3.声明预览控制器
@property (nonatomic, strong) QLPreviewController *preViewController;4.遵守协议和数据源QLPreviewControllerDelegate, QLPreviewControllerDataSource
核心代码:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // 创建预览控制器 self.preView = [[QLPreviewController alloc] init]; // 设置代理和数据源 self.preView.delegate = self; self.preView.dataSource = self; [self.preView setCurrentPreviewItemIndex:0]; [self presentViewController:self.preView animated:YES completion:nil]; }#pragma mark QLPreviewControllerDataSource- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller { return 1;}- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{ NSString *docu = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *filePath = [docu stringByAppendingPathComponent:@"004.png"]; NSURL *url = [NSURL fileURLWithPath:filePath]; return url; }
- iOS开发之UIDocumentInteractionController
- 【iOS功能实现】之利用UIDocumentInteractionController打开和预览文档
- iOS预览PDF文档 UIDocumentInteractionController
- UIDocumentInteractionController
- iOS 使用UIDocumentInteractionController类打开文件
- iOS UIDocumentInteractionController 第三方打开详解
- 之利用UIDocumentInteractionController打开和预览文档
- 文件查看之UIDocumentInteractionController和UIWebView.WKWebView
- UIDocumentInteractionController之程序间文档共享
- iOS利用UIDocumentInteractionController和Quick Look打开或预览文档
- iOS UIDocumentInteractionController 设置第三方打开方式的 参数详解
- UIDocumentInteractionController -iOS预览、打开文档(doc、pdf等)
- iOS 调起第三方程序打开文件 ( UIDocumentInteractionController )
- UIDocumentInteractionController的使用之打开和预览文档
- 8. UIDocumentInteractionController
- iOS 两种预览使用详解(配Demo下载)( Quicklook& UIDocumentInteractionController)
- iOS 第三方应用中打开自己的文件(UIDocumentInteractionController)
- IOS开发之IOS生命周期
- iOS 给【控制器添加统计】的简单、使用方法
- Spark入门实战系列--5.Hive(上)--Hive介绍及部署
- STM32定时器---正交编码器模式详解
- forum | futaba
- hdu 2594 Simpsons’ Hidden Talents
- iOS开发之UIDocumentInteractionController
- shape用法
- VMWar 安装Centos 7 (32位) 出现丢失网卡设备的解决方法
- 漫漫Android路,上下而求索!
- SpringMVC中使用Interceptor拦截器
- document.cookie.indexof的解释
- 登录页面
- 如何运行带有pod的项目
- POJ 3415:Common Substrings 后缀数组+单调栈