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;    }

0 0