Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the current full URL for WKWebView

Is there a way to get the FULL URL loaded by a WKWebView for every request?

webView:didFinishNavigation:

Works only for mainFrame navigations and does not provide a URL request parameter.

How do I get the FULL URL just like in UIWebViewDelegate's

webViewDidFinishLoad:webView

...which gets invoked after any loading finishes and you can get the full request URL from the webView parameter.

It's nice that WKWebView's URL property saves the work that needs to be done to extract a UI-friendly base URL, but it's a huge loss we can't get the full one!

I have tried using

webView:decidePolicyForNavigationAction:decisionHandler:

...but it produces different results for URLs compared to what a UIWebView's request property holds after finishing the load of a page.

like image 429
i-konov Avatar asked Oct 13 '14 14:10

i-konov


People also ask

How do I know if WKWebView is loaded?

To check if your WKWebView has loaded easily implement the following method: import WebKit import UIKit class ViewController: UIViewController, WKNavigationDelegate { let webView = WKWebView() func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)

Is WKWebView the same as Safari?

WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.

How do I load a URL in WKWebView swift 5?

You need to turn the string into a URL , then put the URL into an URLRequest , and WKWebView will load that. Fortunately it's not hard to do! Warning: Your URL must be complete, and valid, in order for this process to work. That means including the https:// part.

What is IOS WKWebView?

WKWebView is part of the WebKit framework: It allows you to embed web content into your app as a seamless part of your app's UI.


3 Answers

You can get URL for a newly requested Webpage by "navigationAction.request.URL" in decidePolicyForNavigationAction delegate method.

func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
   if let urlStr = navigationAction.request.URL?.absoluteString{
      //urlStr is what you want, I guess.
   }
   decisionHandler(.Allow)
}
like image 115
Yuichi Kato Avatar answered Oct 24 '22 00:10

Yuichi Kato


First, I think you are confusing NSURL and NSURLRequest. The first is readily accessibly via webView.URL and it does actually give you the full URL of whatever was loaded. Assuming that where you say URL you mean NSURL.

If that is not what you meant, for example if you wanted to see the redirect chain or the response headers, then I'm afraid the answer is that you cannot get to tht specific information via the WKWebView.

You will have to fall back to UIWebView where you can intercept requests relatively easily and see the full request/response.

like image 22
Stefan Arentz Avatar answered Oct 23 '22 23:10

Stefan Arentz


This is Yuichi Kato's answer for Swift 4. It retrieves the full URL from the request property of the navigation action in the webView(_:decidePolicyFor:decisionHandler:) method of WKNavigationDelegate.

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if let urlStr = navigationAction.request.url?.absoluteString {
        //urlStr is what you want
    }

    decisionHandler(.allow)
}

Don't forget to conform your class to WKNavigationDelegate and set your web view's delegate accordingly:

class WebViewController: UIViewController, WKNavigationDelegate

[...]

webView.navigationDelegate = self
like image 13
Tamás Sengel Avatar answered Oct 23 '22 22:10

Tamás Sengel