iOS利用UIDocumentInteractionController和Quick Look打开或预览

在App的开发过程中,我们避免不了要打开软件中的文件,例如:Excel文件,,Word文件,图片文件等不同格式的文件或者想要通过第三方的App来打开这些文件,那么我们就要用到UIDocumentInteractionController和Quick Look来解决这些问题了。

一、UIDocumentInteractionController的使用方法 – 首先创建一个UIDocumentInteractionController对象,并对该对象初始化一个URL作为文件路径

1、首先要遵循UIDocumentInteractionControllerDelegate2、其次是创建一个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]];//为该对象初始化一个加载路径_docControllernavRect =;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{;}- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{;}

二、UIDocumentInterRactionController使用时的注意事项

1、UIDocumentInterRactionController定义的时候一定要是retain类型的,因为必须要对该对象进行持有;

2、当选择显示包含预览的菜单项 [_docController presentOptionsMenuFromRect:navRectinView:self.viewanimated:YES];时应该注意一点,该方法会触发该类的内置打印输出,会将日志信息打印出来,从而导致App崩溃严重的可能会导致手机死机,现在还未找到解决方法。类似输出结果如下(此处省略该输出的后面部分内容,因为太多了):

unknown activity items supplied: ({ffd8ffe0 ffdb0043 a07 ……

当上述方法确实无法满足你的要求的时候就可以考虑一下用Quick Look了。

三、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];

5、代理方法

– (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{ return 1;}- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{ NSString *fileName = [self.listDicobjectForKey:@”fileName”];NSString* path = [NSHomeDirectory()stringByAppendingPathComponent:[NSStringstringWithFormat:@”Documents/%@”,fileName]];return [NSURLfileURLWithPath:path];}

孤独是一种无奈的选择,孤独是因为没有找到合适的同行者,

iOS利用UIDocumentInteractionController和Quick Look打开或预览

相关文章:

你感兴趣的文章:

标签云: