1
votes

I would like to replicate the behaviour of the Facebook app, where when you click in an external link, it opens up in a new browser (but it's not in Safari).

I currently have a WKWebView that loads my website. I can catch the user on click events and open them, let's say in Safari. I now want to, instead of opening them in Safari, create a new WKWebView and display that link there...

import UIKit
import Darwin
import SafariServices
import WebKit

class ViewController: UIViewController, UIWebViewDelegate,    SFSafariViewControllerDelegate, WKNavigationDelegate {

let url = "http://myurl:3000"
var webView : WKWebView!
var webView2 : WKWebView!

@IBOutlet weak var progressView: UIProgressView!
var myTimer = Timer()
var theBool: Bool = false

override func viewDidLoad() {
    super.viewDidLoad()
    let url = URL(string: self.url)!
    webView.load(URLRequest(url: url) as URLRequest)
    webView.allowsBackForwardNavigationGestures = true
}

override func loadView() {
    webView = WKWebView()
    webView.navigationDelegate = self
    view = webView
}

func webViewDidFinishLoad(_ webView: UIWebView) {
    theBool = true;
}

func webViewDidStartLoad(_ webView: UIWebView) {
    myTimer = Timer.scheduledTimer(timeInterval: 0.06, target: self, selector: #selector(ViewController.updateTimer), userInfo: nil, repeats: true)
    theBool = false;
    progressView.progress = 0.0
}

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if navigationAction.navigationType == .linkActivated  {
            webView2 = WKWebView()
            webView2.navigationDelegate = self
            view = webView2
            let url = navigationAction.request.url
            webView2.load(URLRequest(url: url!) as URLRequest)

    } else {
        print("not a user click")
        decisionHandler(.allow)
    }
}

func updateTimer()
{
    if (theBool) {
        if (progressView.progress >= 1) {
            progressView.isHidden = true;
            myTimer.invalidate()
        }
        else {
            progressView.progress += 0.1;
        }
    }
    else {

        if (progressView.isHidden){
            progressView.isHidden = false
        }
        progressView.progress += 0.05;
        if (progressView.progress >= 0.60) {
            progressView.progress = 0.60;
        }
    }
}

}

The problem is that, even though it is building, when I click on the external link, the app crashes.

libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

1
decisionHandler(.cancel) is missing after webView2.load call. Please checkSri Veera Raghava Ala

1 Answers

1
votes

I recommend you to implement this WKUIDelegate method and load the URL in new WKWebView inside this method.

optional func webView(_ webView: WKWebView, 
createWebViewWith configuration: WKWebViewConfiguration, 
              for navigationAction: WKNavigationAction, 
   windowFeatures: WKWindowFeatures) -> WKWebView?