UIDocumentInteractionController是OC語言的一個(gè)類,但是他并不是一個(gè)controller,而是一個(gè)繼承自NSObject類
一、主要作用:
1.預(yù)覽類似pdf、doc、ppt等類型文件的類。
2.可以將用戶接收到的文件分享到用戶手機(jī)上的其他App中。
二、使用方式
1.預(yù)覽文件,并可以分享文件,如圖1
2.直接分享文件,如圖2
三、使用方法:
1.創(chuàng)建一個(gè)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.設(shè)置分享代理
? ? _document.delegate? ? =? (id)self;
3.哪類文件支持第三方打開,這里不證明就代表所有文件!
//? ? _document.UTI = @"com.microsoft.word.doc";
4.判斷手機(jī)中有沒有應(yīng)用可以打開該文件并打開分享界面
// 用戶預(yù)覽文件,如圖1所示
//BOOL canOpen = ? [_document presentPreviewAnimated:YES];
// 用戶不預(yù)覽文件直接分享,如圖2所示
? ? BOOL canOpen = [self.document presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];
? ? if(!canOpen) {
? ?NSLog(@"沒有程序可以打開選中的文件");
? ? }
5.出現(xiàn)如圖2所示說明調(diào)用成功,點(diǎn)擊想要分享的應(yīng)用即可。
但是如果出現(xiàn)點(diǎn)擊了沒有反應(yīng)的話那就有可能是iOS11版本的時(shí)候,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.將文件復(fù)制到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 {
給用戶提示當(dāng)前沒有其他應(yīng)用可打開該文檔
}
5.執(zhí)行以上第2條之后的代碼。
幾個(gè)代理的使用
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller{
? ? return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller {
? ? return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller {
? ? return self.view.frame;
}
//點(diǎn)擊預(yù)覽窗口的“Done”(完成)按鈕時(shí)調(diào)用
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController*)controller {
}
// 文件分享面板彈出的時(shí)候調(diào)用
- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController*)controller{
? ? NSLog(@"WillPresentOpenInMenu");
}
// 當(dāng)選擇一個(gè)文件分享App的時(shí)候調(diào)用
- (void)documentInteractionController:(UIDocumentInteractionController*)controller willBeginSendingToApplication:(nullableNSString*)application{
? ? NSLog(@"begin send : %@", application);
}
// 彈框消失的時(shí)候走的方法
-(void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController*)controller{
? ? NSLog(@"dissMiss");
}
圖1 用戶預(yù)覽文件示意圖

圖2 用戶分享文件

由于代碼過于簡單,所以不提供代碼下載,有需要的可以直接把相關(guān)代碼拷貝到工程里。
以上是我使用UIDocumentInteractionController的心得,如有不足請多指教,謝謝!