1

I am displaying a web page in WKWebView. It loads successfully. On the page there are 3 buttons - X, Y, Z. When X and Y are tapped, the web view changes content as it should. When Z is tapped nothing happens. I tried the same link in Safari Mobile. X and Y work the same way as in the WKWebView. Z redirects to another page. The last doesn't happen in the app. What can be wrong? Thank you in advance.

To load the displayed page I am using:

if let url = URL(string: <My_URL>) {
    let request = URLRequest(url: url)
     wkWebView.load(request)
}

NSAllowsArbitraryLoads is set to true

surToTheW
  • 772
  • 2
  • 10
  • 34
  • Check this: "Set this key’s value to YES to disable App Transport Security (ATS) restrictions for all domains not specified in the NSExceptionDomains dictionary. Domains you specify in that dictionary aren’t affected by this key’s value." - https://developer.apple.com/documentation/bundleresources/information_property_list/nsapptransportsecurity/nsallowsarbitraryloads – Oscar Fernandez May 08 '19 at 14:06
  • @OscarFernandez I had a look. I disabled it so that if there are any restrictions they don't apply. But no change - still not redirected to the next page. – surToTheW May 08 '19 at 14:33

2 Answers2

1

Have you implemented the WKNavigationDelegate methods?

When tapped the Z button, this method should be called (otherwise, maybe something in your WEB code or WKWebView configuration is wrong)

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    decisionHandler(.allow)
}

If everything happens correctly, you can investigate some issue by these ones

func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error)

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error)
Sore
  • 176
  • 4
  • Hi, thank you for your reply. It turned out Z was opening a new tab in the browser which was not handled by default by the `WKWebView`, so this led me to the decision to open it in a browser and to here: https://stackoverflow.com/a/33808811/3066272 – surToTheW May 09 '19 at 08:53
1

You need to this implement WKUIDelegate method -

    webView.uiDelegate = self   // set this before loading the original url, e.g. in viewDidLoad() of vc

Then add this method in your view controller -

    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
       // Ideally you should create another WKWebView on the fly and provide that web view instance in return statement
       // WebKit automatically loads the target url in this webview.
       return webView
    }

Source: https://developer.apple.com/documentation/webkit/wkuidelegate

Ashok
  • 6,224
  • 2
  • 37
  • 55
  • Thanks to your answer I found this post (https://stackoverflow.com/questions/39070722/wkwebview-wont-open-social-media-login-popups) where there is an implementarion of createWebViewWith, quite helpful. – Joule87 Mar 10 '22 at 13:50