[英]Load local html files from applications's document directory swift wkwebview
I am trying to load a local html file which is downloaded to my application with wkwebview. 我正在尝试加载本地html文件,该文件已通过wkwebview下载到我的应用程序中。 Along with this html file i am downloading two js files, they are referenced inside the html file.
与此HTML文件一起,我正在下载两个js文件,它们在html文件中被引用。
But what ever i do, they are not loading in wkwebview 但是,无论我做什么,他们都没有加载到wkwebview中
@IBOutlet var webView: WKWebView!
@IBOutlet var act: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
// Adding webView content
webView.uiDelegate = self
webView.navigationDelegate = self
let fm = FileManager.default
let docsurl = try! fm.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let myurl = docsurl.appendingPathComponent("index.html")
print("my url = ",myurl )
// myurl prints file///var/some directories/index.html
let request = URLRequest(url: myurl!)
webView.load(request)
}
My html file 我的html文件
<!DOCTYPE html>
<html>
<head>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
But it is not loading on the screen from safari(Develop/) i can see that An error occured trying to load the resource
, page is coming as about:blank 但是它没有从safari(Develop /)加载到屏幕上,我可以看到
An error occured trying to load the resource
,页面即将出现:空白
swift 4.2 迅捷4.2
//load HTML
let bundle = Bundle.main
var path = bundle.path(forResource: "index", ofType: "html")
var html = ""
do {
try html = String(contentsOfFile: path!, encoding: .utf8)
} catch {
//ERROR
}
let urlstr = "http://website.com"
webViewWK.loadHTMLString(html, baseURL: URL(string: urlstr)!)
URLRequest
is basically made for live url. URLRequest
基本上是为实时URL制作的。 For system file just load the URL
. 对于系统文件,只需加载
URL
。 You need to use below method and it will work: 您需要使用以下方法,它将起作用:
/*! @abstract Navigates to the requested file URL on the filesystem.
@param URL The file URL to which to navigate.
@param readAccessURL The URL to allow read access to.
@discussion If readAccessURL references a single file, only that file may be loaded by WebKit.
If readAccessURL references a directory, files inside that file may be loaded by WebKit.
@result A new navigation for the given file URL.
*/
@available(iOS 9.0, *)
open func loadFileURL(_ URL: URL, allowingReadAccessTo readAccessURL: URL) -> WKNavigation?
So that: 以便:
self.webView.loadFileURL(myurl, allowingReadAccessTo: myurl)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.