附件方面的东西程序员iOS Swift && Objective-C

iOS的本地文件预览

2017-06-08  本文已影响818人  见哥哥长高了

有时候根绝项目开发的需要,我们需要预览本地的文件(word、Excel、pdf等)。
例如:


显示效果

那么其实现方式有哪些呢?
iOS的本地文件预览主要有以下三种方式。

一、UIDocumentInteractionController####

     //本地文件的绝对路径
     NSString *path = [[NSBundle mainBundle] pathForResource:@"接口文档3.30号" ofType:@"docx"];
     NSURL *url = [NSURL fileURLWithPath:path];
     
     UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL:url];
     interactionController.delegate = self;
     
     //预览有其他软件打开按钮
     [interactionController presentPreviewAnimated:NO];
     
     CGRect navRect = self.navigationController.navigationBar.frame;
     navRect.size =CGSizeMake(1500.0f,40.0f);
     
     //直接显示包含预览的菜单项
     [interactionController presentOpenInMenuFromRect:navRect inView:self.view animated:YES];

我们设置了interactionController.delegate = self;就要实现UIDocumentInteractionControllerDelegate中其相应的代理方法

-(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    return self;
}
-(UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
    return self.view;
}
-(CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
    return  self.view.frame;
}

command+R,我们就能看到实现效果了

二、quickLook####

quickLook也是系统提供的
首先导入#import <QuickLook/QuickLook.h>

        QLPreviewController *qlController = [[QLPreviewController alloc]init];
        qlController.delegate = self;
        qlController.dataSource = self;
        [qlController setCurrentPreviewItemIndex:0];
        [self presentViewController:qlController animated:YES completion:nil];

接下来实现QLPreviewControllerDelegate,QLPreviewControllerDataSource中的相关方法:

/*!
 * @abstract Returns the number of items that the preview controller should preview.
 * @param controller The Preview Controller.
 * @result The number of items.
 */
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
    return 1;
}

/*!
 * @abstract Returns the item that the preview controller should preview.
 * @param controller The Preview Controller.
 * @param index The index of the item to preview.
 * @result An item conforming to the QLPreviewItem protocol.
 */
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"接口文档330号" ofType:@"docx"];
    NSURL *url = [NSURL fileURLWithPath:path];
    return url;
}

这样,就能实现文件的预览了

三,UIWebView / WKWebView####

现以webView为例,来实现文件预览,我具体实现细节如下:

    _webView.delegate = self;
    _webView.multipleTouchEnabled = YES;
    _webView.scalesPageToFit = YES;
    _webView.scrollView.bounces = NO;
    NSStringEncoding *useEncodeing = nil;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"接口文档330号" ofType:@"docx"];
    NSString *body = [NSString stringWithContentsOfFile:path encoding:(long)useEncodeing error:nil];
    if (body) {
        
        body =[body stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];//替换换行符为HTML换行符
        
        [_webView loadHTMLString:body baseURL:[NSURL fileURLWithPath:path]];
        
    }else { //其他文件直接加载请求
        
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
        [_webView loadRequest:request];
    }

webView加载完成调用:

- (void)webViewDidFinishLoad:(UIWebView *)webView

{
    //获取加载的h5字符串的body
    
    NSString *lJs = @"document.documentElement.innerText";//获取当前网页的html
    
    NSString *currentHTMLBody = [webView stringByEvaluatingJavaScriptFromString:lJs];
    
    if ([currentHTMLBody isEqualToString:@""]) { //不能预览
        
        //做一些不能预览的处理操作
    }
    ;
}
上一篇 下一篇

猜你喜欢

热点阅读