1

What would the equivalent of

webView.delegate = self

Be in a WKWebView? I have

webView2.UIDelegate = self

But it does not work as intended.

9
  • There is the navigationDelegate which allows you to handle page load events. Commented May 12, 2016 at 17:42
  • Doesn't let me use tap gestures Commented May 13, 2016 at 14:52
  • What exactly do you want to do? Add a tap gesture recognizer to interact with the web view HTML content? Commented May 13, 2016 at 15:11
  • Add a tap gesture to add a border around the view Commented May 13, 2016 at 15:18
  • When the view is tapped Commented May 13, 2016 at 16:12

2 Answers 2

7

You can monitor WKWebView's canGoBack and canGoForward, using key-value observing.

SWIFT 3

Add two key-value observers to WKWebView.

let webView = WKWebView(frame: view.frame)
view.addSubview(webView)

webView.addObserver(self, forKeyPath: #keyPath(WKWebView.canGoBack), options: .new, context: nil)
webView.addObserver(self, forKeyPath: #keyPath(WKWebView.canGoForward), options: .new, context: nil)

Now, implement observeValue(forKeyPath:) method in your view controller.

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

    if keyPath == #keyPath(WKWebView.canGoBack) || keyPath == #keyPath(WKWebView.canGoForward) {
        // Do Something...
    }
}

For example, you can add something like this to the change tint color of the buttons.

backButton.imageView?.tintColor = webView.canGoBack ? UIColor.blue : UIColor.gray
forwardButton.imageView?.tintColor = webView.canGoBack ? UIColor.blue : UIColor.gray

I found this method by reading an article from here.

https://www.hackingwithswift.com/example-code/wkwebview/how-to-monitor-wkwebview-page-load-progress-using-key-value-observing

Sign up to request clarification or add additional context in comments.

Comments

0

Are you sure the only change to the code is changing UIWebView to WKWebView? I have made a sample project and the tap gesture recognizer worked for both UIWebView and WKWebView. Here is the relevant code:

class ViewController: UIViewController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let webView = WKWebView(frame: view.frame)
        view.addSubview(webView)
        webView.translatesAutoresizingMaskIntoConstraints = false
        let urlString = "https://www.google.com"
        let url = NSURL(string: urlString)!
        let urlRequest = NSURLRequest(URL: url)
        webView.loadRequest(urlRequest)

        let recognizer = UITapGestureRecognizer(target: self, action: #selector(webViewTapped))
        recognizer.delegate = self
        webView.addGestureRecognizer(recognizer)
    }

    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }

    func webViewTapped(recognizer: UITapGestureRecognizer) {
        print("Tapped")
    }
}

1 Comment

It must be in my functions following the view being tapped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.