0

in Xcode, I am working on a browser. (for desktop)

So far, I have a basic one. I am very new to Xcode.

How do I make the TextField (where you type the URL) update to what the URL the web viewer is currently viewing?

For example, if you type in stackoverflow.com in chrome and start clicking on stuff, the text field above will change and not stay as stackoverflow.com like mine does.

Thanks!

MULTI Team
  • 43
  • 8

1 Answers1

0

You should implement WKNavigationDelegate's decidePolicyFor:navigationAction:decisionHandler: and retreive the URL there.

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping ((WKNavigationActionPolicy) -> Void)) {
    textField.text = navigationAction.request.url?.absoluteString

    decisionHandler(.allow)
}

Note: you must apply the delegate to the WKWebView, like this:

webView.navigationDelegate = self
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223