【发布时间】:2019-01-29 08:02:49
【问题描述】:
我正在尝试加载一个本地 html 文件,该文件使用 wkwebview 下载到我的应用程序中。除了这个 html 文件,我还下载了两个 js 文件,它们在 html 文件中被引用。
但是无论我做什么,他们都没有在 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)
}
我的 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>
但它没有从 safari(Develop/) 加载到屏幕上,我可以看到 An error occured trying to load the resource,页面即将到来:空白
【问题讨论】:
-
myurl打印什么?您在这里如何使用WKWebView? -
But it is not loading on the screen from safari(Develop/)是什么意思? Safari 是外部浏览器。你的意思是WebView,而不是Safari? -
这段代码
let request = URLRequest(url: myurl) self.webView.load(request)没有问题。您需要通过从该 url 创建 Data 对象来检查文件是否存在于同一 url。 -
@TheTiger,是的,它在我的模拟器中运行良好,但在 iPhone(真实设备)中无法运行
-
您的接受率为 0。您尚未接受所有问题的任何一个答案。这不好。