Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture redirect url in wkwebview in ios

How do I capture the redirection url in when using WKWebView like if a webpage redirects to another page on submitting the username and password or some other data. I need to capture the redirected url. Is there any method in WKNavigationDelegate to override?

like image 357
Prabu Raj Avatar asked Aug 10 '17 04:08

Prabu Raj


2 Answers

Use this WKNavigationDelegate method

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

Hope this helps

like image 139
Reinier Melian Avatar answered Nov 05 '22 07:11

Reinier Melian


(This answers the slightly more general question of how to detect a URL redirection in WKWebView, which is the search that lead me to this page.)

Short answer

Use WKNavigationDelegate's webView(_:didReceiveServerRedirectForProvisionalNavigation:) function and examine WKWebView's URL property.

Longer answer

There are a couple of places you could detect a server-side redirect.

On iOS 10.3.3 and iOS 11.0, the sequence of events I observe when loading a URL that gets redirected by the server is:

  1. The WKNavigationDelegate function webView(_:decidePolicyFor:decisionHandler:) is called for the original URL request. WKWebView's URL property is set to the original URL.

  2. The WKNavigationDelegate function webView(_:didStartProvisionalNavigation:) is called for the original URL request. WKWebView's URL property is set to the original URL.

  3. The WKWebView's URL property is updated by WebKit to the redirection URL. (You'll only know about this if you are key-value observing the property.)

  4. The WKNavigationDelegate function webView(_:decidePolicyFor:decisionHandler:) is called for the redirected URL request. WKWebView's URL property is then redirection URL.

  5. The WKNavigationDelegate function webView(_:didReceiveServerRedirectForProvisionalNavigation:) is called. WKWebView's URL property is the redirection URL.

(Note: On the iOS 11.0 simulator I have seen steps 3 and 4 reversed, with the URL property unchanged in webView(_:decidePolicyFor:decisionHandler:), which actually seems like a sensible ordering, but I haven't observed this on a device.)

It seems like the webView(_:didReceiveServerRedirectForProvisionalNavigation:) is built explicitly for the purpose of detecting redirects so is probably the preferred option, although the redirect could be possibly be inferred at steps 3 or 4 but only if you can be sure that there are no other causes of navigational change.

like image 41
user2067021 Avatar answered Nov 05 '22 06:11

user2067021