我们这里说的是利用UIDocumentInteractionController进行文件的预览和分享
注:file_urlshi是文件的存储路径,因为应用沙盒路径会变,所以建议获取的时候不要使用绝对路径
-(void)openFileViewController: (NSString *) file_url {
NSURL *file_URL = [NSURL fileURLWithPath:file_url];
if (file_URL != nil) {
if (_documentController == nil) {
_documentController = [[UIDocumentInteractionController alloc] init];
_documentController = [UIDocumentInteractionController interactionControllerWithURL:file_URL];
_documentController.delegate = self;
}else {
_documentController.URL = file_URL;
}
[_documentController presentPreviewAnimated:YES];
}
}
实现UIDocumentInteractionController的三个代理方法
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)interactionController
{
return self;
}
-(UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
NSLog(@"documentInteractionControllerDidEndPreview");
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
NSLog(@"documentInteractionControllerDidDismissOpenInMenu");
return self.view.frame;
}
2,也可以用下面的方法直接调出应用分享不预览
NSString *filePath =xxxxxxxx;
self.documentController =
[UIDocumentInteractionController
interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
self.documentController.delegate = self;
self.documentController.UTI = @"com.adobe.pdf";
[self.documentController presentOpenInMenuFromRect:CGRectZero
inView:self.view
animated:YES];