The WKWebView is not loading links. I am linking users to a privacy policy page, and the page has a group of links. The links are all pdfs hosted by wix. On safari and Chrom
First you should add this to your delegate:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(WKNavigationActionPolicy.allow)
}
Then you may also need to implement the following method if the link has target="_blank"
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
Likely, you are using target="_blank" in your anchor tag. That opens up a new window to display the link. WKWebView is blocking your attempt to open a new window (at least by default).
The code below still does not create a new window, but instead opens the PDF, etc link in the current WKWebView. The other option seems to be to create a new WKWebView and return it, so that ios can open the link in that. I don't want extra Views being created by every click on a website inside the WKWebView.
In your ViewController.viewDidLoad
webView.uiDelegate = self
Then add the extension for the delegate
extension ViewController: WKUIDelegate {
/**
* Force all popup windows to remain in the current WKWebView.
* By default, WKWebView is blocking new windows from being created
* ex <a href="link" target="_blank">text</a>.
* This code catches those popup windows and displays them in the current WKWebView.
*/
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
// open in current view
webView.load(navigationAction.request)
// don't return a new view to build a popup into (the default behavior).
return nil;
}
}
I still don't know what the true cause was, but when I changed the hosting of the files from Wix to S3, I stopped getting the issue.
There are a few things you can check:
Verify that the <a href
links doesn't contain target="_blank"
attribute since WKWebView doesn't know how to open the link in a new tab. see https://stackoverflow.com/a/25853806/810466 for how to work around that.
Check if the link is HTTPS or update the App Transport Security Settings
with the Allow Arbitrary Loads
option
Make sure that you start the loading request only after adding the WKWebView to the view hierarchy in didMoveToParentViewController:
since it may make javascript to fail if it tries to run outside the view hierarchy
Implement the WKWebView NavigationDelegate methods and make sure you return WKNavigationActionPolicyAllow
when deciding the policy for the request
One thing I can suggest (not sure if it will help, but too long for comment) is try to implement WKNavigationDelegate's decidePolicyFor
:
// WKNavigationDelegate
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
decisionHandler(.allow)
}
If it helps, then make that function more granular, i.e. which navigation you need to enable just for PDFs, not everything else in the world.