iOS利用UIDocumentInteractionController和Quick Look打開或預(yù)覽文檔

打開軟件中的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];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容