打開軟件中的Word文件可能存在特殊格式的文件無法顯示,我們可以選擇使用其他應(yīng)用打開,有時圖片文件等不同格式的文件或者想要通過第三方的App來打開這些文件,我們就要用到UIDocumentInteractionController和Quick Look來解決這些問題了。
一、UIDocumentInteractionController的使用方法
- 首先創(chuàng)建一個UIDocumentInteractionController對象,并對該對象初始化一個URL作為文件路徑
1、首先要遵循UIDocumentInteractionControllerDelegate
2、其次是創(chuàng)建一個UIDocumentInteractionController對象
@property(nonatomic,retain)UIDocumentInteractionController *docController;
3、在方法中進行UIDocumentInteractionController對象的初始化
- (void)open{
NSString *fileName = [self.listDicobjectForKey:@"fileName"];
NSString* path = [NSHomeDirectory()stringByAppendingPathComponent:
_docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];//為該對象初始化一個加載路徑
_docController.delegate =self;//設(shè)置代理
//直接顯示預(yù)覽
// [_docController presentPreviewAnimated:YES];
CGRect navRect =self.navigationController.navigationBar.frame;
navRect.size =CGSizeMake(1500.0f,40.0f);
//顯示包含預(yù)覽的菜單項
[_docController presentOptionsMenuFromRect:navRectinView:self.viewanimated:YES];
//顯示不包含預(yù)覽菜單項
//[docController presentOpenInMenuFromRect:navRect inView:self.view animated:YES];
}
4、代理方法
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self;
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
{
return self.view.frame;
}
二、UIDocumentInterRactionController使用時的注意事項
<code>
UIDocumentInterRactionController定義的時候一定要是strong類型的,因為必須要對該對象進行持有;
當(dāng)上述方法確實無法滿足你的要求的時候就可以考慮一下用Quick Look了。
</code>
三、Quick Look的使用方法
1、首先要添加QuickLook.FrameWork框架。
2、在需要用到的Controller中添加頭文件#import
- (void)open{
QLPreviewController *myQlPreViewController = [[QLPreviewController alloc]init];
myQlPreViewController.delegate =self;
myQlPreViewController.dataSource =self;
[myQlPreViewController setCurrentPreviewItemIndex:0];
[self presentViewController:myQlPreViewControlleranimated:YEScompletion:nil];
3、代理方法
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
return 1;
}
- (id)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
NSString *fileName = [self.listDicobjectForKey:@"fileName"];
NSString* path = [NSHomeDirectory()stringByAppendingPathComponent:[NSStringstringWithFormat:@"Documents/%@",fileName]];
return [NSURLfileURLWithPath:path];
}