0

In my app, I am opening a URL using WK WebView. URL loads correctly, but if I click on some external links in the loaded URL it does not redirect to other websites.

For example

Following is my URL https://www.urlaubstracker.de

In the above link, different holiday deals are linked to external websites. If I click on any button which redirects user to another website, app does not work or load the URL.

I tried using UI WebView and it is working perfectly fine. Could some one please point out what exactly I am missing here in my WK WebView.

Waleed Asif
  • 159
  • 3
  • 15
  • Http is not a issue here, as in my app in info.plist I have already did "App Transport Security Settings - Allows Arbitrary Loads to YES". – Waleed Asif Jun 29 '17 at 10:58
  • Add NSExceptionAllowsInsecureHTTPLoads = YES may be it will help you.check my answer https://stackoverflow.com/a/44795602/3937664 – Dharma Jun 29 '17 at 11:10

2 Answers2

0
  1. First, enable ATS in your app.

  2. Now, please try with this code.

    var webView: WKWebView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let webViewConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: view.bounds, configuration: webViewConfiguration)
        let url = URL(string: "https://www.urlaubstracker.de")
        let req = NSURLRequest(url: url!)
        self.webView!.load(req as URLRequest)
        self.webView?.navigationDelegate = self
        webView?.navigationDelegate = self
        webView?.allowsBackForwardNavigationGestures = true
        webView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(webView!)
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, 
    decisionHandler: (@escaping (WKNavigationActionPolicy) -> Void)) {
    print("webView:\(webView) decidePolicyForNavigationAction:\(navigationAction) decisionHandler:\(decisionHandler)")
    
    
        switch navigationAction.navigationType {
        case .linkActivated:
            if navigationAction.targetFrame == nil {
                webView.load(navigationAction.request)
            }
        default:
            break
        }
    
        decisionHandler(.allow)
    }
    
Adrian
  • 16,233
  • 18
  • 112
  • 180
  • @Waleed Asif, I had tried like this and it is working fine. – rohit chauhan Jun 29 '17 at 11:09
  • I used exactly your code, but its not working. It loads the URL correctly but whenever I click on the link which should open another website, nothing happens and thats my issue. Using UI Webview its working fine bt not with WK web view that I would like to use as it is recommended by Apple. Could you please help here – Waleed Asif Jun 29 '17 at 12:19
  • Could you please help me to replicate the same? I have loaded your website with Webkit. Now, where should I click? – rohit chauhan Jun 29 '17 at 12:30
  • For example in this URL,https://www.urlaubstracker.de/italien-kalabrien-residence-san-leo/ If you try to click on "flug", Wk Webview does not open it (clicking "flug" user is taken on a other website if I use UI Webview). – Waleed Asif Jun 29 '17 at 12:49
  • @waleed Asif, I have updated my answer please look up to this. – rohit chauhan Jul 04 '17 at 11:16
0

try using WKNavigationDelegate

also switch to SafariViewController

RinaLiu
  • 400
  • 2
  • 9