Capturing URL redirection in wkwebview in ios

How to capture a redirect URL when using WKWebView, for example, if a web page is redirected to another page when sending a username and password or some other data. I need to grab the redirected URL. Is there any method in WKNavigationDelegate to override?

+4
source share
3 answers

Use this method WKNavigationDelegate

public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void)
    {
        if(navigationAction.navigationType == .formSubmitted)
        {
            if navigationAction.request.url != nil
            {
                //do what you need with url
                //self.delegate?.openURL(url: navigationAction.request.url!)
            }
            decisionHandler(.cancel)
            return
        }
        decisionHandler(.allow)
    }

Hope this helps

+5
source

(This is the answer to a slightly more general question on how to determine URL redirection in WKWebView, which is the search that leads me to this page.)

Short answer

WKNavigationDelegate webView(_:didReceiveServerRedirectForProvisionalNavigation:) WKWebView URL.

, .

iOS 10.3.3 iOS 11.0 , URL-, :

  • WKNavigationDelegate webView(_:decidePolicyFor:decisionHandler:) URL-. WKWebView URL URL.

  • WKNavigationDelegate webView(_:didStartProvisionalNavigation:) URL-. WKWebView URL URL.

  • WKWebView URL WebKit URL- . ( , - .)

  • WKNavigationDelegate webView(_:decidePolicyFor:decisionHandler:) URL. WKWebView URL URL .

  • WKNavigationDelegate webView(_:didReceiveServerRedirectForProvisionalNavigation:) . WKWebView URL - URL .

(: iOS 11.0 3 4 , URL webView(_:decidePolicyFor:decisionHandler:), , .)

, webView(_:didReceiveServerRedirectForProvisionalNavigation:) , , , , 3 4, , .

+4

Use the webView: shouldStartLoadWithRequest: navigationType: delegate method.

redirects fall under UIWebViewNavigationTypeOther.

-1
source

Source: https://habr.com/ru/post/1683390/


All Articles