WKWebView 无法在 HTML 中加载本地资源
WKWebView cannot load local resource in HTML
我从本地应用程序 "Documents" 文件夹加载了 HTML 个页面,其中包含结构化目录。 "main"文件夹存放主页和资源,"interactive"文件夹存放另一个模块,通过主页重定向。
我在 "main" 文件夹中加载 "ipad.html",页面无法加载资源(即 css / js 文件)以显示正确的样式。代码是这样的。
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.")
}
里面 "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>
但我将其移至 Bundle.main,并删除 "main" 文件夹以加载 "ipad.html" 正确显示。为什么它有不同的行为或 iOS 不支持 "Documents" 文件夹下的复杂文件夹结构,我应该如何更正它?谢谢。
这个简单的在 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.")
}
}
在您的项目中添加所有 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
}
我从本地应用程序 "Documents" 文件夹加载了 HTML 个页面,其中包含结构化目录。 "main"文件夹存放主页和资源,"interactive"文件夹存放另一个模块,通过主页重定向。
我在 "main" 文件夹中加载 "ipad.html",页面无法加载资源(即 css / js 文件)以显示正确的样式。代码是这样的。
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.")
}
里面 "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>
但我将其移至 Bundle.main,并删除 "main" 文件夹以加载 "ipad.html" 正确显示。为什么它有不同的行为或 iOS 不支持 "Documents" 文件夹下的复杂文件夹结构,我应该如何更正它?谢谢。
这个简单的在 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.")
}
}
在您的项目中添加所有 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
}