WKWebView加载本地文件

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];
复制代码

你可能感兴趣的:(json)