美文网首页iOS开发
iOS开发:UIWebView加载pptx,docx,xlsx等

iOS开发:UIWebView加载pptx,docx,xlsx等

作者: chasitu | 来源:发表于2018-11-08 17:20 被阅读84次

    这段时间项目需求需要加载各种格式类型的文件,以前这样的问题我们直接可以用wkwebview加载文件URL就可以了,但是遇到了很多的问题,例如:wkwebview加载pptx,docx等新版office文件格式的时候加载不出来,试了各种加载方式都无果,最终选择了老版的UIwebview,注释之前所有wkwebview相关代码,webview其它的使用细节我这里就不多说了,说核心部分

    首先,就是UIWebview的这个方法最终解决了我的问题
    - (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;
    
    加载方法
    - (void)loadDataWithURL:(NSURL *)url MIMEType:(NSString  *)MIMEType
    {
        WS(weakSelf);
        [SVProgressHUD showWithStatus:Localized(@"MainLoading")];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSData *data = [NSData dataWithContentsOfURL:url];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.webview loadData:data MIMEType:MIMEType textEncodingName:@"UTF-8" baseURL:url];
            });
        });
    }
    

    ( 1 ) 这里获取二进制数据使用了GCD异步加载,因为这行代码非常耗时,特别是网络较慢的时候,
    ( 2 ) 每种格式文件的MIMEType都不一样,所以抽取了方法
    ( 3 ) baseURL是加载二进制文件的URL,方法内部用该URL获取文件相关格式

    下面给大家看UIwebview和WKWebview的两个效果图
    WKWenview加载图
    UIWebView效果图

    注:同一个文件UIWebView正常加载,WKWebView加载乱码,具体的原因时间问题没有深究,

    相关MIMEType如下
    mimetypes mimes
    image/jpeg jpg
    image/jpeg jpeg
    image/png png
    image/webp webp
    application/vnd.ms-excel xls
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
    application/msword doc
    application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
    application/vnd.openxmlformats-officedocument.presentationml.presentation pptx
    application/vnd.ms-powerpoint ppt
    application/pdf pdf
    text/plain txt

    相关文章

      网友评论

        本文标题:iOS开发:UIWebView加载pptx,docx,xlsx等

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