我在加载本地HTML文件时遇到问题。 这是我的代码。 请帮忙。
let URL = NSBundle.mainBundle().pathForResource("index2", ofType:"html")
let request = NSURLRequest(URL: url!)
Webview.loadRequest(request)
通过使用以下代码。 我已经设法加载了html文件,但没有加载CSS!
让htmlFile = NSBundle.mainBundle()。pathForResource(" index1",ofType:" html")
让html =试试? 字符串(contentsOfFile:htmlFile !,编码:NSUTF8StringEncoding)
GSFWebView.loadHTMLString(html !, baseURL:nil)
如何将本地html文件加载到UIWebView中的可能重复项
编辑有关您的问题的详细信息的问题。 您发布的代码可能甚至无法编译。 从这些错误开始。
只需在您的ViewDidLoad中编写此代码,我就以添加此页面的URL为例,只需将其替换为您的URL。 在情节提要上,您无需执行任何操作,如果您在视图控制器上添加了任何额外的视图作为网站的容器,则用您的视图替换我代码中的self.view。
let myWebView:UIWebView = UIWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
//add your url on line below
myWebView.loadRequest(URLRequest(url: URL(string:"https://stackoverflow.com/questions/26647447/load-local-html-into-uiwebview-using-swift")!))
self.view.addSubview(myWebView)
而且,如果要从本地文件加载页面,可以通过这种方式定义url来完成。
迅捷2
let url = NSBundle.mainBundle().URLForResource("webFileName", withExtension:"html")
迅捷3
let url = Bundle.main.url(forResource:"webFileName", withExtension:"html")