UIDocumentInteractionController

UIDocumentInteractionController分享文件

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

UIDocumentInteractionController
通过AirDrop来共享文件

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

二、分享文件实现步骤
首先我们要声明一个类,必须是全局的!!不然后面会发生崩溃

// 定义,并遵循代理
@interface ViewController ()<UIDocumentInteractionControllerDelegate>
@property (nonatomic, strong) UIDocumentInteractionController *documentController;
@end

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

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

实现代理方法

#pragma mark - ** UIDocumentInteractionController 代理方法 **
- (UIViewController )documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController )controller{
return self;
}
- (UIView)documentInteractionControllerViewForPreview:(UIDocumentInteractionController)controller {
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller {
return self.view.frame;
}

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

UIDocumentInteractionController
QuickLook.framework

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

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

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

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

UIDocumentInteractionController
右上角共享

配置中遇到的问题:选*享的应用之后,应用闪退!报错信息如下:(原因是因为没有把类设置为全局导致提前释放了)

 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

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

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

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

UIDocumentInteractionController
配置我们支持的文档

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

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