我們這里說的是利用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];
}
}
實現(xiàn)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];