以我的經驗,如果你想要的地方的網絡資源,你必須混合:
加載下載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