首页 文章

如何在本地WKWebView中加载iframe?

提问于
浏览
0

我正在尝试实现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 加载麻烦并抛出以下错误:

收到来自网络过程中的意外网址:“文件:///var/containers/Bundle/Application/160FB6DE-E1E2-47AD-A715-202EF5C5C1FB/SampleApp.app/Bookshelf/Books/BookName/content/footer.html” 2018年4月16日18:57:16.049559-0700 SampleApp [5047:5710785]所获从Web处理的无效消息 “WebPageProxy.DecidePolicyForNavigationAction” . 从Web进程收到无效消息“WebPageProxy.DecidePolicyForNavigationAction” .

以下是用于加载页面的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时似乎不起作用 . 我做错了什么,或者错过了什么?谢谢 .

1 回答

  • 0

    我假设这不是本地网站文件,而且这些东西是在Web服务器上 . 您可以将 "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.
        }
    }
    

相关问题