ios webView 打开 ppt pdf

项目中最近添加了新功能,从第三方app 分享文件过来,并展示出来

相关MIMEType如下

mimetypesmimes
image/jpegjpg
image/jpegjpeg
image/pngpng
image/webpwebp
application/vnd.ms-excelxls
application/vnd.openxmlformats-officedocument.spreadsheetml.sheetxlsx
application/msworddoc
application/vnd.openxmlformats-officedocument.wordprocessingml.documentdocx
application/vnd.openxmlformats-officedocument.presentationml.presentationpptx
application/vnd.ms-powerpointppt
application/pdfpdf
text/plaintxt

代码实现

 NSLog(@"filpath------%@", filpath);
    NSData *Data = [NSData dataWithContentsOfFile:_filePath];
    
    NSString *type = [filpath componentsSeparatedByString:@"."].lastObject;
    
    if ([type isEqualToString:@"docx"])
    {
        [self.webView loadData:Data MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
        
    }else if ([type isEqualToString:@"doc"])
    {
        [self.webView loadData:Data MIMEType:@"application/msword" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
        
    }else if([type isEqualToString:@"txt"])
    {
        [self.webView loadData:Data MIMEType:@"text/plain" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
        
        
    }else if([type isEqualToString:@"PDF"] || [type isEqualToString:@"pdf"])
    {
        [self.webView loadData:Data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
        
    }else if([type isEqualToString:@"PNG"] || [type isEqualToString:@"png"])
    {
        [self.webView loadData:Data MIMEType:@"image/png" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
        
    
    }else if([type isEqualToString:@"ppt"])
    {
        [self.webView loadData:Data MIMEType:@"application/vnd.ms-powerpoint" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
        
    }else if ([type isEqualToString:@"pptx"])
    {
        [self.webView loadData:Data MIMEType:@"application/vnd.openxmlformats-officedocument.presentationml.presentation" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
        
    }else if ([type isEqualToString:@"xlsx"])
    {
        [self.webView loadData:Data MIMEType:@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
        
    }else if ([type isEqualToString:@"xls"])
    {
        [self.webView loadData:Data MIMEType:@"application/vnd.ms-excel" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
        
    }
    else{
    }

 

 

 

 

    原文作者:On--The--Way
    原文地址: https://blog.csdn.net/investzhu/article/details/100178545
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞