以我的经验,如果你想要的地方的网络资源,你必须混合:
加载下载HTML,然后使用loadHTMLString
加载它,使用本地文件系统的baseURL
。当URL是服务器端URL时,我没有成功加载本地资源。我一直不得不下载HTML并直接加载它。
所有网络资源需要完全限定(即使用“http:// ...”)。由于我们要将基本URL设置为本地文件系统,并且服务器代码上的本地URL引用不起作用。他们必须完全合格。
不限iPhone本地源可以本地引用(即,没有任何file://
或http://
前缀),或者也可以通过基本URL作为HTTP请求参数并执行服务器端脚本以嵌入此唯一的网址。
因此,我的iOS代码如下所示:
// get my bundle local URL
NSURL *bundleUrl = [[NSBundle mainBundle] bundleURL];
// download the html from the server
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// load that html into my web view using a the bundle as the base URL
[self.webView loadHTMLString:html baseURL:bundleUrl];
所以,对于这个工作,我的HTML可能看起来像:
<html>
<body>
<table>
<tr>
<td>image from iOS file system using local reference</td>
<td><img src="sample.png" /></td>
</tr>
<tr>
<td>server image</td>
<td><img src="http://myserverurl.com/sampleonserver.png" /></td>
</tr>
</table>
</body>
</html>
这里我使用img
标签,但我怀疑你的标签也是如此。我还假设你的本地引用在bundle中,但是如果它们在Documents文件夹中,我认为它是等价的。只要确保所有本地引用都指向本地文件系统内的相对目录,并且所有服务器请求都是完全限定的。
来源
2013-03-25 02:34:43
Rob