美文网首页
2019-03-04 iOS 8/9 系统 使用WKWebvie

2019-03-04 iOS 8/9 系统 使用WKWebvie

作者: Cocoa_Coder | 来源:发表于2019-03-04 15:46 被阅读0次

    编辑于 2019-03-04
    iOS 8/9 系统 使用WKWebview ,针对本地加载HTML代码问题(loadHTMLString:baseURL:)
    问题:加载本地图片
    iOS 10以后 通过下面的代码可以解决加载本地图片

     NSString *path = [[NSBundle mainBundle] resourcePath];
    NSURL *baseURL = [NSURL fileURLWithPath:path];
     [self.webView loadHTMLString:HtmlString baseURL:baseURL];
    
    

    iOS 8/9 系统 :

    NSURL *temDirURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:@"www"];
    
    [self.webView loadHTMLString:HtmlString baseURL:temDirURL];
    
    

    iOS 8/9 系统需要将本地资源copy 一份到 沙盒下的tmp文件夹下:

    //将图片资源copy 到 tmp 文件夹下
    - (void)copyResourcesToTMPFolder{
        //js文件
        [self copyResourcesToTMPFolderWithFileName:@"jquery.min" fileType:@"js"];
      //png 文件
        [self copyResourcesToTMPFolderWithFileName:@"comment_icon" fileType:@"png"];
    }
    -(void)copyResourcesToTMPFolderWithFileName:(NSString*)fileName fileType:(NSString *)fileType{
        NSString *copyPath = [[NSBundle mainBundle] pathForResource:fileName ofType:fileType];
        [self copyFileWithPath:copyPath];
    }
    //复制文件到 tmp 文件夹下
    - (void)copyFileWithPath:(NSString *)path{
        
        NSFileManager * fileManager= [NSFileManager defaultManager];
        //创建文件夹路径
        NSURL * temDirURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:@"www"];
        //是否是文件夹
        BOOL isDir;
        BOOL isExit = [fileManager fileExistsAtPath:temDirURL.absoluteString isDirectory:&isDir];
        //文件夹是否存在
        if (!isExit || !isDir) {
            [fileManager createDirectoryAtURL:temDirURL withIntermediateDirectories:YES attributes:nil error:nil];
        }
        //     NSLog(@"temDirURL : %@",temDirURL.absoluteString);
        //判断数据库文件是否存在
        NSString * temDirURLpath = [temDirURL.absoluteString stringByReplacingOccurrencesOfString:@"file:///" withString:@"/"];
        //    NSLog(@"temDirURL : %@",temDirURLpath);
        NSString * filePath = [temDirURLpath stringByAppendingPathComponent:[[path componentsSeparatedByString:@"/"] lastObject]];
        //    NSLog(@"filePath : %@",filePath);
        //如果文件不存在,则复制
        if (![fileManager fileExistsAtPath:filePath]) {
            NSError * error = nil;
            BOOL isSuccess = [fileManager copyItemAtPath:path toPath:filePath error:&error];
            NSLog(@"文件%@", isSuccess ? @"拷贝成功" : @"拷贝失败");
            if(error){
                NSLog(@"%@",error.description);
            }
        }
    }
    

    另外,一些绝对地址,还需要替换

    + (NSString *)setingDefaultspPath:(NSString *)path{
        if (![CommonFunction isIOS10AndLater]) {
            NSURL * temDirURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:@"www"];
            NSString * temDirURLpath = [temDirURL.absoluteString stringByReplacingOccurrencesOfString:@"file:///" withString:@"/"];
            return  [temDirURLpath stringByAppendingPathComponent:[[path componentsSeparatedByString:@"/"] lastObject]];
        }else{
            return path;
        }
    }
    //用法
    
            NSString *defaultpath = [[NSBundle mainBundle] resourcePath];
            defaultpath = [defaultpath stringByAppendingPathComponent:@"userweb_headImage_defaultImage.png"];
            defaultpath = [CommentHtmlManager setingDefaultspPath:defaultpath];//这里替换地址
            
              [commentList appendFormat:@"<div style=\"display:inline-block;float:left;\"><img data-original=\"%@\" originsrc=\"%@\" class=\"headerimage\" onError=\"LoadDefaultHeaderImage();\" defaultImagesrc=\"%@\" src=\"%@\" onclick=\"showotheruserinfo(%ld,\\'%@\\');\"/>",[CommentHtmlManager GetAvatarAddress:[NSString stringWithFormat:@"%ld",userid]],[CommentHtmlManager GetAvatarAddress:[NSString stringWithFormat:@"%ld",userid]],defaultpath,defaultpath,userid,[[nickname stringByReplacingOccurrencesOfString:@"%5c" withString:@"%5c%5c"] stringByReplacingOccurrencesOfString:@"%27" withString:@"%5c%27"]];
            
           
    

    参考链接:
    https://www.jianshu.com/p/868dee57a0ef
    https://www.jianshu.com/p/1688c11af71c

    相关文章

      网友评论

          本文标题:2019-03-04 iOS 8/9 系统 使用WKWebvie

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