UIWebView链接到本地​​文件,而无需将baseURL设置为app目录

Ant*_*tox 6 uiwebview ios

我在一个应用程序中有一个Web视图,从服务器加载一个包含文本和图像的html片段.我将它包装在一个小的HTML中并将其放入这样的Web视图中:

NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"style" ofType:@"css"];

NSString *cssLink = [NSString stringWithFormat:@"<link href='file://%@/' type='text/css' rel='stylesheet'/>", cssPath];

NSString *html = [[NSString stringWithFormat:@"<html><head><title></title><meta charset='UTF-8' />%@</head><body>%@</body></html>", cssLink, htmlContent] retain];

[myWebView loadHTMLString:html baseURL:currentURL];
Run Code Online (Sandbox Code Playgroud)

有没有办法修复上面的代码,以便链接到本地​​样式表?

如果我将基本URL更改为本地应用程序资源文件,我可以毫无问题地加载样式表,但这会破坏从服务器加载的html中的图像链接.

我还可以将样式表的内容直接包含在html中,但由于多种原因不愿意这样做.

谢谢.

Wil*_*Niu 2

什么是currentURL?应该是[[NSBundle mainBundle] bundleURL],例如

[webview loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
Run Code Online (Sandbox Code Playgroud)

我相信 cssPath 可以只是文件名,而不是绝对路径。