简体   繁体   English

WKWebView无法加载HTML中的本地资源

[英]WKWebView cannot load local resource in HTML

I have HTML pages load from the local app "Documents" folder, with structured directories. 我从本地应用程序“ Documents”文件夹中加载了具有结构化目录的HTML页面。 The "main" folder store home page and resource, an "interactive" folder store another module, through homepage to redirect. “主”文件夹存储主页和资源,“交互”文件夹存储另一个模块,可以通过主页进行重定向。

在此处输入图片说明

I load the "ipad.html" in "main" folder, the page cannot load resources (ie css / js files) to display correct style. 我在“主”文件夹中加载了“ ipad.html”,该页面无法加载资源(即css / js文件)以显示正确的样式。 The code like this. 这样的代码。

let indexHTML = "ipad.html"
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentDirectoryPath: String = path[0]
let folderPath = documentDirectoryPath.appending("/main")
let destinationURLForFile = URL(fileURLWithPath: folderPath + "/\(indexHTML)")
let baseUrl = URL(fileURLWithPath: folderPath, isDirectory: true)

do{
    let fileName =  try String(contentsOf: destinationURLForFile, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
    webView.loadHTMLString(fileName, baseURL: baseUrl)

}catch{
    print("Loading HTML failed.")
}

Inside "ipad.html" like this. 在“ ipad.html”中是这样的。

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>demo</title>
    <meta name="description" content="" />
    <meta name="author" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">

    <link rel="stylesheet" href="./css/animate.min.css">
    <link rel="stylesheet" href="./css/style.css">

    <script src="./bower_components/modernizr/modernizr-2.5.3.min.js"></script>
</head>
<body class="page-ipad-landing animated" lang="">
......
......
</body>
</html>

But I move it to Bundle.main, and remove "main" folder to load the "ipad.html" is display correctly. 但是我将其移至Bundle.main,并删除“ main”文件夹以加载“ ipad.html”,这是正确显示的。 Why it different behavior or iOS not support complex folder structure under "Documents" folder and how should I correct it? 为什么它有不同的行为,或者iOS在“ Documents”文件夹下不支持复杂的文件夹结构,我该如何纠正? Thanks. 谢谢。

This simple one is work properly in Bundle.main folder. 这个简单的文件可以在Bundle.main文件夹中正常工作。

if let url = Bundle.main.url(forResource: "ipad", withExtension: "html") {
  do {
       let contents = try String(contentsOfFile: url.path)
       webView.loadHTMLString(contents, baseURL: url.deletingLastPathComponent())
  } catch {
     print("Could not load the HTML string.")
  }
}

Add all the HTML directories & files in your project. 在您的项目中添加所有HTML目录和文件。

let webView = WKWebView()
if let htmlPath = Bundle.main.path(forResource: "directory/index", ofType: "html") {
  let htmlUrl = URL(fileURLWithPath: htmlPath, isDirectory: false)
  webView.loadFileURL(htmlUrl, allowingReadAccessTo: htmlUrl)
  view = webView
}

在此处输入图片说明

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

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