你好,我有WkWebview应用程序,可以加载网页这是我的wkwebview 初始化代码
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
preferences.setValue(true, forKey: "DeveloperExtrasEnabled")
webConfiguration.preferences = preferences
browser = WKWebView(frame: .zero, configuration: webConfiguration)
browser.navigationDelegate = self
browser.uiDelegate = self
view = browser
}
这是请求
let url = URL(string: urlString)
request = URLRequest(url: url!,cachePolicy: .reloadIgnoringLocalAndRemoteCacheData,timeoutInterval: 360)
browser.navigationDelegate = self
browser.uiDelegate = self
browser.load(request)
这很好,但当我点击加载时间超过60秒的子页面时(这是默认的请求超时间隔(。我在web检查器中收到此错误加载资源失败:请求超时所以我的问题是,我可以更改所有请求的默认timeoutInterval吗?
PS:我确实试图覆盖navigationDelegate方法。
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { }
阻止连接并用新的请求加载子页面,但它不起作用。
PS:在Safari中,仅在WKWebview中按预期加载的子页面不起作用。
感谢您的回答或建议
当第二次调用webView(_:decidePolicyFor:decisionHandler:)
函数时,您需要处理这种情况。(从您修改的URLRequest
到新的timeoutInterval
(
类似的东西会起作用:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.request.timeoutInterval != 360 {
var urlRequest = navigationAction.request
urlRequest.timeoutInterval = 360
decisionHandler(.cancel)
webView.load(urlRequest)
} else {
decisionHandler(.allow)
}
}