1
我有一個web視圖,成功顯示本地html文件。 css文件和imgage與html文件和應用程序文件位於同一文件夾中。 我試過三種不同的方法來顯示html,但沒有顯示css或圖像。圖像由帶問號的小方塊表示。uiwebview也讀取css文件或圖像
這是加載功能
func loadPage() {
// 1st try
// if let htmlFile = NSBundle.mainBundle().pathForResource(page, ofType: "html"){
//let htmlData = NSData(contentsOfFile: htmlFile)
//let baseURL = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath)
// webView.loadData(htmlData, MIMEType: "text/html", textEncodingName: "UTF-8", baseURL: baseURL)
// 2nd try - - - - - - -
// let localfilePath = NSBundle.mainBundle().URLForResource(page, withExtension: "html");
//let myRequest = NSURLRequest(URL: localfilePath!);
//webView.loadRequest(myRequest);
var testHTML = NSBundle.mainBundle().pathForResource(page, ofType: "html")
var contents = NSString(contentsOfFile: testHTML!, encoding: NSUTF8StringEncoding, error: nil)
var baseUrl = NSURL(fileURLWithPath: testHTML!) //for load css file
webView.loadHTMLString(contents, baseURL: baseUrl)
//}
}
的HTML是
<meta charset="utf-8">
<link rel="stylesheet" href="bg.css" type="text/css">
<h1>1 ornano car park</h1>
<p class= "test"> At the end of the coach park there's a circle where coaches can turn. From there turn your back on the cathedral and go towards the river.</p>
<img src="board.jpg">
如果我把樣式文件中,它的工作原理,但我想有20個HTML頁面,所以我想用一個css文件。
爲了試圖瞭解發生了什麼,我添加了一個println()來顯示路徑。
var testHTML = NSBundle.mainBundle().pathForResource(page, ofType: "html")
println(testHTML)
var testCSS = NSBundle.mainBundle().pathForResource("bg", ofType: "css")
println(testCSS)
它打印
Optional("/Users/colinmcgarry/Library/Developer/CoreSimulator/Devices/7BAEC0D1-5942-4D60-AD4F-CCFF4C70BB0B/data/Containers/Bundle/Application/42AAF91D-8477-428A-AEC6-950D89846C80/TestWebView2.app/1page.html")
nil
的bg.css文件是在同一文件夾中的HTML文件,並在列表中關閉應用程序文件。 xcode似乎不存在。
奇怪的是,您的問題通過正確簡潔地描述需要發生的一切來提供答案。謝謝 :) – Carl 2015-03-31 15:48:11