Reputation: 157
In my wkwebview I have a link to some file like pages, numbers etc. I see the wknavigationresponse decidepolicy to make a decision to download or show the content. However the wknavigation response object does not seem to contain the requested URL but apple modified url. for example I request
https://www.sarvepalli.net/ax/Subscriptions%20Data.numbers
It gets translated to in wknavigationresponse.response.url like below:
x-apple-ql-id://4A35794C-554C-4732-9159-BFCB7A688F1D/x-apple-ql-magic/Subscriptions%20Data.numbers
I tired using the wknavigationaction to set a global variable called current_url but that does not seem to work as these methods are being either asynchronously called or called in reverse order than I had imagined (1) Wknavigationaction then (2) decidepolicyfor: WknavigationResponse
I am stuck any help in finding a way to collect the current URL being requested when a user clicks on a link in webview, it will be greatly appreciated.
Thanks Vijay
/* This should get replaced by the navgationaction */
var current_url= URL(string: "https://example.com/download/a.docx")!
func webView(_ webView: WKWebView,
createWebViewWith configuration: WKWebViewConfiguration,
for navigationAction: WKNavigationAction,
windowFeatures: WKWindowFeatures) -> WKWebView? {
// if navigationAction.targetFrame == nil, let url = navigationAction.request.url {
if navigationAction.request.url != nil, let url = navigationAction.request.url {
//if url.description.lowercased().range(of: "http://") != nil ||
self.current_url = url
let url_request:URLRequest = URLRequest(url: navigationAction.request.url!)
webView.load(url_request)
}
func webView(_ webView: WKWebView,
decidePolicyFor navigationResponse: WKNavigationResponse,
decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
let mimetype = navigationResponse.response.mimeType!
let thisurl = navigationResponse.response.url?.absoluteString
print("Current Url being requested is ",current_url.absoluteString)
print("Current Url being considered ", navigationResponse.response.url?.absoluteString ?? "unknown");
decisionHandler(.allow)
}
I get strange response like
Current Url being requested is printed as "https://example.com/download/a.docx" (This should be acctually be the URL being clicked which is https://www.sarvepalli.net/ax/Subscriptions%20Data.numbers )
Current Url being considered x-apple-ql-id://4A35794C-554C-4732-9159-BFCB7A688F1D/x-apple-ql-magic/Subscriptions%20Data.numbers (This is an unusable internal URL specified by apple which is perhaps being cached somewhere in the device)
Upvotes: 1
Views: 2852
Reputation: 157
I solved this issue. Looks like if I force a navigationpolicy lookup then this wkwebview delegate gets called every time and everything works fine like I expcted.
func webView(_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
// if navigationAction.targetFrame == nil, let url = navigationAction.request.url {
if navigationAction.request.url != nil, let url = navigationAction.request.url {
self.curl = url
decisionHandler(.allow)
}
}
Now self.curl the global variable gets updated with every URL call.
Thanks! Vijay
Upvotes: 2