下面是在WKWebview中给出空白屏幕的代码。已尝试通过添加
- webView(_ webView:WKWebView,didFailProvisionalNavigation导航:WKNavigation!,withError错误:错误)
- webView(_ webView:WKWebView,decidePolicyFor navigationResponse:WKNavigationResponse,decisionHandler:@escaping(WKNavigationResponsePolicy)
但它也没有给予任何错误。
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var container: UIView!
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var logTextView: UITextView!
var webView: WKWebView?
var javascript: Javascript?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var webFrame = self.container!.frame
webFrame.origin.x = 0
webFrame.origin.y = 0
// Create a configuration for the preferences
let config = WKWebViewConfiguration()
config.defaultWebpagePreferences.allowsContentJavaScript = true
self.webView = WKWebView(frame: webFrame, configuration: config)
self.webView?.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
self.container!.addSubview(webView!)
let testUrl: URL! = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "dist")
self.webView!.loadFileURL(testUrl, allowingReadAccessTo: testUrl)
self.javascript = Javascript(webView: self.webView!)
self.webView?.navigationDelegate = self
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print(error.localizedDescription)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse,
decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
if let response = navigationResponse.response as? HTTPURLResponse {
print(response.statusCode)
if response.statusCode == 401 {
// ...
}
}
decisionHandler(.allow)
}
1条答案
按热度按时间t30tvxxf1#
非常简单,但很难找到-在index.html中,我们有像“src="/js/chunk-xxx.78xe86ad.js”这样的链接, 你需要去掉链接中的第一个“/”。工作起来像一个魅力!!!