WKWebView無法載入本地路徑的資原始檔,因為資源路徑需要手動設定,下面程式碼中將canvas資料夾設為baseUrl,這樣就可以將本地載入的檔案放到該資料夾下,為了適配src="./xxxx"
.
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) configuration:[[WKWebViewConfiguration alloc] init]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString *htmlStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSString *assetPath = [NSBundle.mainBundle.bundlePath stringByAppendingPathComponent:@"canvas"];
NSURL *baseUrl = [NSURL fileURLWithPath:assetPath];
[webView loadHTMLString:htmlStr baseURL:baseUrl];
[self.view addSubview:webView];
複製程式碼