UIDocumentInteractionController分享文件

一、背景
我们的应用下载了一个文件,格式是我们自身不支持打开的,怎么办!Apple中提供了一种方法,可以使用第三方所支持打开这种格式的App来打开,如下效果图:

通过AirDrop来共享文件

那我们应该怎样来设置这种效果呢,跟着下面的步骤,你就可以轻易地实现了:

二、分享文件实现步骤
首先我们要声明一个类,必须是全局的!!不然后面会发生崩溃
<pre>
// 定义,并遵循代理
@interface ViewController ()<UIDocumentInteractionControllerDelegate>
@property (nonatomic, strong) UIDocumentInteractionController *documentController;
@end
</pre>

在我们需要触发共享文件的方法里面实现下面的代码:
<pre>

  • (void)buttonOnClick:(UIButton*)btn{
    //初始化 filePath:为所要打开的文件的路径
    _documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
    _documentController.delegate = self;// 遵循代理
    _documentController.UTI = @"com.microsoft.excel.xls"; // 哪类文件支持第三方打开,这里不证明就代表所有文件!
    [_documentController presentOpenInMenuFromRect:CGRectZero
    inView:self.view
    animated:YES];
    }
    </pre>

实现代理方法
<pre>

pragma mark - ********** UIDocumentInteractionController 代理方法 **********

  • (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    return self;
    }
  • (UIView)documentInteractionControllerViewForPreview:(UIDocumentInteractionController)controller {
    return self.view;
    }
  • (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller {
    return self.view.frame;
    }
    </pre>

最后在Build Phases中的Link Binary中导进QuickLook.framework这个库

QuickLook.framework

最后我们就可以通过我们的的方法来共享文件出去了。

------------------- 分割线 -------------------
当然,我们也可以通过下面的方法,直接打开文档,UIDocumentInteractionController本身也支持部分文档的预览,例如Word、excel、js等等:
<pre>

  • (void)readButtonOnClick:(UIButton*)btn{
    //初始化 filePath:为所要打开的文件的路径
    _documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
    _documentController.delegate = self;// 遵循代理
    // 直接打开预览文档
    [_documentController presentPreviewAnimated:YES];
    }
    </pre>
预览文档

遇到无法打开的文档的时候,我们还可以共享

右上角共享

配置中遇到的问题:选中共享的应用之后,应用闪退!报错信息如下:(原因是因为没有把类设置为全局导致提前释放了)
<pre>
*** Assertion failure in -[_UIOpenWithAppActivity performActivity], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.29.5/UIDocumentInteractionController.m:408
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIDocumentInteractionController has gone away prematurely!'
*** First throw call stack:
(0x248e185b 0x35fa2dff 0x248e1731 0x25672ddb 0x290638c9 0x292695bb 0x28d5aefd 0x28d5e1a1 0x28b42107 0x28a50a55 0x28a50531 0x28a5042b 0x282e05cf 0x1acd03 0x1b17c9 0x248a4535 0x248a2a2f 0x247f50d9 0x247f4ecd 0x2db6aaf9 0x28a7e2dd 0x780ad 0x366f0873)
libc++abi.dylib: terminating with uncaught exception of type NSException
</pre>

三、接收别的App传过来的文件

我们能够通过把文件传给别的应用打开,这样子那我们的App要怎样做才能支持别的程序传文件过来打开呢?

下面我们以支持打开PDF为例子:其他文档类型可以去在官网找https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html

配置我们支持的文档

在AppDelegate.m文件中,重写以下方法,就可以获取到传过来的内容:

<pre>

  • (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
    if (url != nil) {
    NSString *path = [url absoluteString];
    NSMutableString *string = [[NSMutableString alloc] initWithString:path];
    if ([path hasPrefix:@"file://"]) {
    [string replaceOccurrencesOfString:@"file://" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, path.length)];
    }
    // 打开PDF文档
    NSlog(@"获取到的文件:%@", string);
    }
    return YES;
    }
    </pre>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,663评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,010评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,974评论 6 342
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,780评论 18 399
  • 4月21日,北京碧水源科技股份有限公司董事长文剑平带队莅临宿城区参加镇村生活污水治理PPP项目签约仪式,标志着宿城...
    宿商网阅读 403评论 0 0