加載本地前端文件夾下的文件(相對路徑加載html中需要的css尔许,js)
- 添加文件時使用引用模式添加么鹤,選擇下圖中紅色標(biāo)記
-
使用下面代碼加載,我試了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 必須設(shè)置正確的文件夾路徑才可以,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設(shè)置nil和具體值都可以
補(bǔ)充一個URL轉(zhuǎn)換規(guī)則余佛,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帶參數(shù)
- 第一種方式適合直接加進(jìn)項目中的文件(沙盒下不可以)
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"
//帶參數(shù)時只能使用這種請求方式柠新,上面的幾種不可以
wkwebview.load(URLRequest.init(url: URL.init(string: path, relativeTo: URL.init(fileURLWithPath: dicPath))!))
- 第二種方法加載本地html帶參數(shù),支持沙盒文件
@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;
}
當(dāng)然肯定還有其他的方法可以實現(xiàn)辉巡,這幾種是我在現(xiàn)在項目中使用的恨憎,經(jīng)過一段時間來看還是比較穩(wěn)定的,在此做個筆記老不寫準(zhǔn)備的demo都找不到了郊楣,如果錯誤歡迎指正憔恳。