Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

wknavigationdelegate - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...