i have a web view that successfully displays a local html file. The css file and imgage are in the same folder as the html file and the app files. I've tried three different methods to display the html, but none show the css or image. The image is represented by a small square with a question mark.
我有一个web view,它成功地显示了一个本地html文件。css文件和imgage与html文件和应用程序文件在同一个文件夹中。我尝试了三种不同的方法来显示html,但是没有一个显示css或图像。图像由一个带有问号的小正方形表示。
this is the loading function
这是加载函数。
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)
//}
}
The html is
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">
If I put the style in the file, it works but I want to have 20 HTML pages so I want to use a css file.
如果我把样式放在文件中,它可以工作,但是我想要有20个HTML页面,所以我想要使用一个css文件。
To try to understand what's happening I added a println() to show the path.
为了理解发生了什么,我添加了一个println()来显示路径。
var testHTML = NSBundle.mainBundle().pathForResource(page, ofType: "html")
println(testHTML)
var testCSS = NSBundle.mainBundle().pathForResource("bg", ofType: "css")
println(testCSS)
Which prints
的打印
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
The bg.css file is in the same folder as the html files and is in the list off files in the app. Doesn't seem to exist for xcode.
bg。css文件和html文件在同一个文件夹中,并且在应用程序中的文件列表中。xcode似乎不存在。
1 个解决方案
#1
0
Found the solution. I've been experimenting with targets. The files weren't in the active target. Printing the path led me to realise that I had TestWebView2 as a Target. keep a clear head.
找到了解决方案。我一直在试验目标。文件没有在活动的目标中。打印这条路径让我意识到我有TestWebView2作为目标。保持清醒的头脑。
#1
0
Found the solution. I've been experimenting with targets. The files weren't in the active target. Printing the path led me to realise that I had TestWebView2 as a Target. keep a clear head.
找到了解决方案。我一直在试验目标。文件没有在活动的目标中。打印这条路径让我意识到我有TestWebView2作为目标。保持清醒的头脑。