繁体   English   中英

uiwebview也不读取css文件或图像

[英]uiwebview nor reading css file or image

我有一个可以成功显示本地html文件的网络视图。 css文件和imgage与html文件和app文件位于同一文件夹中。 我尝试了三种显示html的方法,但是没有一种显示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似乎不存在。

找到了解决方案。 我一直在尝试目标。 文件不在活动目标中。 打印路径使我意识到我已经将TestWebView2作为目标。 保持头脑清醒。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM