加载本地前端文件夹下的文件(相对路径加载html中需要的css,js)
- 添加文件时使用引用模式添加,选择下图中红色标记
-
使用下面代码加载,我试了iOS10-12都是可以的。
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都找不到了,如果错误欢迎指正。