surToTheW
surToTheW

Reputation: 832

WKWebView - not loading a page from the web view

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

Upvotes: 1

Views: 6480

Answers (2)

Ashok
Ashok

Reputation: 6244

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

Upvotes: 1

Sore
Sore

Reputation: 186

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)

Upvotes: 2

Related Questions