UIDocumentInteractionController是OC语言的一个类,但是他并不是一个controller,而是一个继承自NSObject类
一、主要作用:
1.预览类似pdf、doc、ppt等类型文件的类。
2.可以将用户接收到的文件分享到用户手机上的其他App中。
二、使用方式
1.预览文件,并可以分享文件,如图1
2.直接分享文件,如图2
三、使用方法:
1.创建一个UIDocumentInteractionController类的属性:
@property (nonatomic,strong)UIDocumentInteractionController * document;
2.遵循UIDocumentInteractionControllerDelegate
@interface OpenAndShareFileViewController () <UIDocumentInteractionControllerDelegate>
3.初始化document
1.指定要分享的链接
NSURL *URL = [[NSBundle mainBundle] URLForResource:@"ios-" withExtension:@"pdf"];
_document = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:cachesDir]];
2.设置分享代理
_document.delegate = (id)self;
3.哪类文件支持第三方打开,这里不证明就代表所有文件!
// _document.UTI = @"com.microsoft.word.doc";
4.判断手机中有没有应用可以打开该文件并打开分享界面
// 用户预览文件,如图1所示
//BOOL canOpen = [_document presentPreviewAnimated:YES];
// 用户不预览文件直接分享,如图2所示
BOOL canOpen = [self.document presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];
if(!canOpen) {
NSLog(@"沒有程序可以打开选中的文件");
}
5.出现如图2所示说明调用成功,点击想要分享的应用即可。
但是如果出现点击了没有反应的话那就有可能是iOS11版本的时候,iOS不支持这样做,需要先把文件拷贝到cache中:
1.获取文件路径
NSString * fileUrl = [[NSBundle mainBundle] pathForResource:@"ios-" ofType:@"pdf"];
2.获取cache路径
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [[paths1 objectAtIndex:0] stringByAppendingPathComponent:fileUrl.lastPathComponent];
3.将文件复制到cache中
NSError*error =nil;
BOOL isCopySuccess = [[NSFileManager defaultManager] copyItemAtPath:fileUrl toPath:cachesDir error:&error];
4.判断如果拷贝成功
if (isCopySuccess) {
初始化_document
_document = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:cachesDir]];
} else {
给用户提示当前没有其他应用可打开该文档
}
5.执行以上第2条之后的代码。
几个代理的使用
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller{
return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller {
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller {
return self.view.frame;
}
//点击预览窗口的“Done”(完成)按钮时调用
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController*)controller {
}
// 文件分享面板弹出的时候调用
- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController*)controller{
NSLog(@"WillPresentOpenInMenu");
}
// 当选择一个文件分享App的时候调用
- (void)documentInteractionController:(UIDocumentInteractionController*)controller willBeginSendingToApplication:(nullableNSString*)application{
NSLog(@"begin send : %@", application);
}
// 弹框消失的时候走的方法
-(void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController*)controller{
NSLog(@"dissMiss");
}
图1 用户预览文件示意图
![](https://img.haomeiwen.com/i15587234/b0312a712055753c.png)
图2 用户分享文件
![](https://img.haomeiwen.com/i15587234/4bb2156463841957.png)
由于代码过于简单,所以不提供代码下载,有需要的可以直接把相关代码拷贝到工程里。
以上是我使用UIDocumentInteractionController的心得,如有不足请多指教,谢谢!
网友评论