美文网首页iOS
iOS利用UIDocumentInteractionContro

iOS利用UIDocumentInteractionContro

作者: 斑驳的流年无法释怀 | 来源:发表于2016-12-25 20:31 被阅读1516次

打开软件中的Word文件可能存在特殊格式的文件无法显示,我们可以选择使用其他应用打开,有时图片文件等不同格式的文件或者想要通过第三方的App来打开这些文件,我们就要用到UIDocumentInteractionController和Quick Look来解决这些问题了。

一、UIDocumentInteractionController的使用方法

  • 首先创建一个UIDocumentInteractionController对象,并对该对象初始化一个URL作为文件路径
1、首先要遵循UIDocumentInteractionControllerDelegate
2、其次是创建一个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;//设置代理

//直接显示预览
//    [_docController presentPreviewAnimated:YES];

CGRect navRect =self.navigationController.navigationBar.frame;
    navRect.size =CGSizeMake(1500.0f,40.0f);

//显示包含预览的菜单项
[_docController presentOptionsMenuFromRect:navRectinView:self.viewanimated:YES];

//显示不包含预览菜单项
//[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类型的,因为必须要对该对象进行持有;
当上述方法确实无法满足你的要求的时候就可以考虑一下用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];

}

相关文章

网友评论

    本文标题:iOS利用UIDocumentInteractionContro

    本文链接:https://www.haomeiwen.com/subject/qgjdvttx.html