How to use an iPhone webView with an external HTML

2019-09-20 13:51发布

问题:

Hi i have an external html on my webserver, that is requested by my iphone app through a webview... in order to make the page load faster, i want to change my html to request static files like css's and images from the local resource folder of my app....

can i do this?

my html is something like this:

<html> <body> 
<img
src="file://Resources/test.png">
</body> </html>

回答1:

You can set the baseURL for UIWebView to the resource path of your main bundle:

NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];

[webView loadHTMLString:htmlString baseURL:baseURL];

Then you can simply refer to your images like this:

<html> <body> 
<img src="test.png" />
</body> </html>