I would like to set a timeout for the web page loaded in the loadRequest of WKWebview.
let configuration=WKWebViewConfiguration()
let webView = WKWebView (frame:self.view.bounds, configuration:configuration)
let req = NSURLRequest (URL: "https://test.com/a.html", cachePolicy: .UseProtocolCachePolicy, timeoutInterval: 10.0)
print("loadRequest=\(req.timeoutInterval)")//->10 seconds
webView.loadRequest(req)
As you can see above, pages loaded with loadRequest are called didFailProvisionalNavigation
Delegate in about 10 seconds as configured.
However, when you tap the link to b.html
on https://test.com/a.html
, decidePolicyForNavigationAction
Delegate is called, so if you look at timeoutInterval
as shown below, it is not the value you set above.
func webView (webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy)->Void){
let request = navigationAction.request
log("decidePolicyForNavigationAction\(request.URL?.absoluteString)=\(request.timeoutInterval)")
// ↑ https://test.com/b.html=2147483647.0 appears.
}
Also, in this case, didFailProvisionalNavigation
Delegate will not return for more than a minute, rather than about 10 seconds above.(I'll be back in about 90-120 seconds.)
Change the UIWebView Read Timeout Value - Qiita
Based on the above site, we tried decidePolicyForNavigationAction
Delegate as follows:
func webView (webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy)->Void){
let request = navigationAction.request
log("decidePolicyForNavigationAction\(request.URL?.absoluteString)=\(request.timeoutInterval)")
// ↑ https://test.com/b.html=2147483647.0 appears.
iflet url=request.URL?absoluteString{
if(request.timeoutInterval!=10){
let req = NSURLRequest (URL: url, cachePolicy:.UseProtocolCachePolicy, timeoutInterval: 10.0)
print("decidePolicyForNavigationAction=\(req.timeoutInterval)")//->10 seconds
webView.loadRequest(req)
decisionHandler (.Cancel)
}
}
}
In this way, the GET process successfully set the timeout period, but the POST process did not pass the parameters and did not result in the intended movement.
swift3 webview with post request-Stack Overflow
Do I have to reconfigure the parameters and reload the loadRequest during POST like the above site?
If anyone knows a better way, I would appreciate it if you could let me know.
Thank you for your cooperation.
swift ios webview
NSURLRequest properties, such as timeoutInterval, are set for requests used in webView.loadRequest(), so the same NSURLRequest is not used for pressing links after loading, so if you want to set the timeout every time, you will need to recreate the NSURLRequest.
© 2024 OneMinuteCode. All rights reserved.