WKWebview 加载本地html,css,js,并且在url后面拼接参数

加载本地前端文件夹下的文件(相对路径加载html中需要的css,js)

  • 添加文件时使用引用模式添加,选择下图中红色标记
  • 使用下面代码加载,我试了iOS10-12都是可以的。


    1559111541741.jpg
      let wkwebview = WKWebView.init(frame: view.bounds)
        view.addSubview(wkwebview)
        
        ///work:html文件名,webviewSdk:html文件所在的文件夹
        var path = Bundle.main.path(forResource: "work", ofType: "html", inDirectory: "webviewSdk") ?? ""
        let dicPath = Bundle.main.bundlePath
//        path = path + "?userId=62154&lessonId=35237&entry=2&token=updatetokenb4666dba-afbb-45cf-aa7b-904a1c3bc8e9"
//        wkwebview.loadFileURL(URL.init(fileURLWithPath: path), allowingReadAccessTo: URL.init(fileURLWithPath: dicPath))
//        wkwebview.load(URLRequest.init(url: URL.init(string: path, relativeTo: URL.init(fileURLWithPath: dicPath))!))//relativeTo 必须设置正确的文件夹路径才可以,nil不可以
//        wkwebview.load(URLRequest.init(url: URL.init(fileURLWithPath: path)))
//        wkwebview.load(URLRequest.init(url: URL.init(string: path)!))///不可以
//        wkwebview.load(URLRequest.init(url: URL.init(fileURLWithPath: path, relativeTo: URL.init(fileURLWithPath: dicPath))))
//        wkwebview.load(URLRequest.init(url: URL.init(fileURLWithPath: path, isDirectory: false)))///isDirectory为true时不行
        wkwebview.load(URLRequest.init(url: URL.init(fileURLWithPath: path, isDirectory: false, relativeTo: nil)))//relativeTo设置nil和具体值都可以

补充一个URL转换规则,AFN里面的
 Below are a few examples of how `baseURL` and relative paths interact:

    NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
    [NSURL URLWithString:@"foo" relativeToURL:baseURL];                  // http://example.com/v1/foo
    [NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL];          // http://example.com/v1/foo?bar=baz
    [NSURL URLWithString:@"/foo" relativeToURL:baseURL];                 // http://example.com/foo
    [NSURL URLWithString:@"foo/" relativeToURL:baseURL];                 // http://example.com/v1/foo
    [NSURL URLWithString:@"/foo/" relativeToURL:baseURL];                // http://example.com/foo/
    [NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/

 Also important to note is that a trailing slash will be added to any `baseURL` without one. This would otherwise cause unexpected behavior when constructing URLs using paths without a leading slash.

加载本地文件夹下的html带参数

  • 第一种方式适合直接加进项目中的文件(沙盒下不可以)
let wkwebview = WKWebView.init(frame: view.bounds)
view.addSubview(wkwebview)
var path = Bundle.main.path(forResource: "work", ofType: "html", inDirectory: "Resource/webviewSdk") ?? ""
let dicPath = Bundle.main.bundlePath
path = path + "?userId=1111&lessonId=0000"
//带参数时只能使用这种请求方式,上面的几种不可以
wkwebview.load(URLRequest.init(url: URL.init(string: path, relativeTo: URL.init(fileURLWithPath: dicPath))!))
  • 第二种方法加载本地html带参数,支持沙盒文件
@parms dicPath 为html文件所在路径
-(NSURL*)p_loadTeacherWorkHtmlPath:(NSString*)dicPath parms:(NSDictionary*)dic{
    NSString *userId = [NSString stringWithFormat:@"%ld",(long)[GZUserMoel shareInstance].userId];
    NSString *token = isStringEmpty([GZCacheManager getCurrentCookie].token)?@"":[GZCacheManager getCurrentCookie].token;
    NSString *userType = @"10";
    NSString *entry = [NSString stringWithFormat:@"%@",dic[@"entry"]==nil?@"":dic[@"entry"]];
    NSString *serial = [NSString stringWithFormat:@"%@",dic[@"serial"]==nil?@"":dic[@"serial"]];
    NSString *lessonId = [NSString stringWithFormat:@"%@",dic[@"lessonId"]==nil?@"":dic[@"lessonId"]];
    
    NSURL *fileUrl = [NSURL fileURLWithPath:dicPath isDirectory:NO];
    NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:fileUrl resolvingAgainstBaseURL:NO];
    [urlComponents setQueryItems:@[[NSURLQueryItem queryItemWithName:@"userId" value:userId],
                                   [NSURLQueryItem queryItemWithName:@"params" value:value],
                                   [NSURLQueryItem queryItemWithName:@"params" value: value],
                                   [NSURLQueryItem queryItemWithName:@"params" value: value],
                                   [NSURLQueryItem queryItemWithName:@"params" value: value],
                                   [NSURLQueryItem queryItemWithName:@"params" value:@"1"],
                                   [NSURLQueryItem queryItemWithName:@"params" value: value],
                                   ]];
    return urlComponents.URL;
}

当然肯定还有其他的方法可以实现,这几种是我在现在项目中使用的,经过一段时间来看还是比较稳定的,在此做个笔记老不写准备的demo都找不到了,如果错误欢迎指正。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS WKWebView 本地HTML、JS、CSS文件加载详解 Tips:NSString类型的文件路径转换为...
    浮生随笔阅读 31,093评论 7 37
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,843评论 1 92
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,161评论 1 32
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,879评论 1 45
  • 沟通力训练营第一期。 故事: 2018年11月1日早晨,泉水宝宝开车上班。上班路上,小伙伴发来作业。泉水宝宝匆匆忙...
    泉水_544e阅读 737评论 0 7