gpt4 book ai didi

html - 如何在本地加载 WKWebView 中的 iframe?

转载 作者:行者123 更新时间:2023-11-30 11:34:53 30 4
gpt4 key购买 nike

我正在尝试实现 WKWebView 来显示以下 html:

<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Appendix B1 - GFACN33 (East coast)</title>
<link href="../../css/bootstrap.min.css" rel="stylesheet">
<link href="../../css/bootstrap-theme.min.css" rel="stylesheet">
<link href="../../css/styles.css" rel="stylesheet">
</head>

<body>
<article class="container-fluid">
<div class="row">

</div>
</article>
<footer>
<iframe src="../../content/footer.html"></iframe>
</footer>
<script src="../../js/jquery-3.1.1.min.js"></script>
<script src="../../js/bootstrap.min.js"></script>
<script src="../../js/MathJax/MathJax.js?config=default.js"></script>
<script src="../../js/script.js"></script>
</body>
</html>

只有 iframe 无法加载并引发以下错误:

Received an unexpected URL from the web process: 'file:///var/containers/Bundle/Application/160FB6DE-E1E2-47AD-A715-202EF5C5C1FB/SampleApp.app/Bookshelf/Books/BookName/content/footer.html' 2018-04-16 18:57:16.049559-0700 SampleApp[5047:5710785] Received an invalid message "WebPageProxy.DecidePolicyForNavigationAction" from the web process.

Received an invalid message "WebPageProxy.DecidePolicyForNavigationAction" from the web process.

以下是用于加载页面的 swift 代码:

import UIKit
import WebKit

let bookInfo = BookInfo.sharedInstance
let quizInfo = QuizInfo.sharedInstance

class AppendixViewController: UIViewController, WKUIDelegate {

var webView: WKWebView!

override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
let htmlPath = URL(fileURLWithPath:
Bundle.main.path(
forResource: quizInfo.questions[quizInfo.curQuiz][quizInfo.curQuestion[quizInfo.curQuiz]].appendix,
ofType: "html",
inDirectory: "Bookshelf/Books/" + bookInfo.owned[bookInfo.bookSelected.ownedId].folder + bookInfo.owned[bookInfo.bookSelected.ownedId].version + "/quiz/appendix")!)
webView.navigationDelegate = self
webView.loadFileURL(htmlPath, allowingReadAccessTo: htmlPath)

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

extension AppendixViewController: WKNavigationDelegate {
// When page finishes loading then update ViewController title
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.title = webView.title
}
}

iframe 正在本地加载,并且位于 html 文件的本地根文件夹之外。我在 UIWebView 中工作得很好,但当我迁移到 WKWebView 时似乎不起作用。我做错了什么,或者错过了什么?谢谢。

最佳答案

我假设这不是本地网站文件,并且这些内容位于网络服务器上。您可以将 "https://example.com" 更改为您想要它指向的任何位置。

import UIKit
import WebKit

class ViewController: UIViewController {
// Outlet For WebKitView On The View Controller
@IBOutlet weak var webview: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = URL(string: "https://example.com")
let request = URLRequest(url: url!)
webview.load(request)
}

// This Will Hide the Status Bar On Iphone X
override var prefersStatusBarHidden: Bool {
return true
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

关于html - 如何在本地加载 WKWebView 中的 iframe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49868777/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com