1 回答

TA貢獻1877條經驗 獲得超1個贊
我已將主包中的共享 HTML 和 javascript 代碼添加為“example.html”,并對您的代碼進行了更改。它在我的最后工作正常。
override func viewDidLoad() {
super.viewDidLoad()
//Url from the html
let url = URL(fileURLWithPath: Bundle.main.path(forResource: "example", ofType: "HTML") ?? "")
// Configuring WKEWebview
let config: WKWebViewConfiguration = WKWebViewConfiguration()
config.userContentController.add(self, name: "test")
webView = WKWebView(frame: self.view.frame, configuration: config)
webView?.navigationDelegate = self
//Constraints related changes
webView.translatesAutoresizingMaskIntoConstraints = true
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.view.addSubview(self.webView)
// Load file
self.webView?.loadFileURL(url, allowingReadAccessTo: Bundle.main.bundleURL)
}
您可以使用加載您的網頁
self.webView.load(URLRequest(url: "https://www.site"))
試試看,如果您遇到任何問題,請告訴我。
添加回答
舉報