UIDocumentInteractionController的
<UIDocumentInteractionControllerDelegate>
@property (nonatomic,strong)UIDocumentInteractionController * 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(@"沒有程序可以打开选中的文件");
}
- (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");
}