最近在项目开发中遇到下载附件文档预览需求,在这里总结一下我的实现方法
这里我总结了三种实现方法(1)用webView预览(2)通过UIDocumentInteractionController实现跳转(3)应用Quick Look系统框架,下面依次介绍各个方法实现
首先来看用webView这个比较常用,不做过多解释,代码如下:
_webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
_webView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:_url1];
[_webView loadRequest:request];
[_webView setScalesPageToFit:YES];
[self.view addSubview:_webView];
第二种应用UIDocumentInteractionController实现方法如下:
//先初始化对象,以及设置弹出方式
_documentInt = [UIDocumentInteractionController interactionControllerWithURL:_url2];
[_documentInt setDelegate:self];
[_documentInt presentPreviewAnimated:YES];
[_documentInt presentOptionsMenuFromRect:CGRectMake(0, 0, 375, 667) inView:self.view animated:YES];
//然后实现相应代理方法
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller
{
return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller
{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{ return self.view.frame;
}
//点击预览窗口的“Done”(完成)按钮时调用
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController*)_controller
{
[_documentInt dismissPreviewAnimated:YES];
}
第三种Quick Look,用这个方法需要导入QuickLook.FrameWork框架,代码如下:
//初始化对象
QLPreviewController *myQlPreViewController = [[QLPreviewController alloc]init];
myQlPreViewController.delegate =self;
myQlPreViewController.dataSource =self;
[myQlPreViewController setCurrentPreviewItemIndex:0];
[self presentViewController:myQlPreViewController animated:YES completion:nil];
//实现代理方法
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
return 1;
}
- (id)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
// NSString *fileName = [self.listDic objectForKey:@"fileName"];
// NSString* path = [NSHomeDirectory()stringByAppendingPathComponent:[NSStringstringWithFormat:@"Documents/%@",fileName]]; return _url3; }