0

I’m loading a some URL content in WebView. The title of navigation item is set to web view title (refer below code) :

extension WebPageView: WKNavigationDelegate {

    func webView(_: WKWebView, didFinish _: WKNavigation!) {
        loader?.hide()
        navigationItem.title = webView?.title
    }

    . . .
}

However, the issue with this is that content of web view is loaded thereafter title of navigation item is set (reported as bug).

Is there any way that title of navigation item is set and then web view content should be loaded?

Jayprakash Dubey
  • 32,447
  • 16
  • 161
  • 169

1 Answers1

2

Well the answer to your question "Is there any way that title of navigation item is set and then web view content should be loaded?" is No, it seems rather impossible, since the "title" of the webview is retrieved once the web content is loaded completely. However

var title: String? { get }

The WKWebView class is key-value observing (KVO) compliant for this property.

You can add observer to webview for key "title" parallel to load request.

webView.load(myRequest())
webView.addObserver(self, forKeyPath: "title", options: .new, context: &myContext)

For more reference https://gist.github.com/fahied/698e6f3a09d898b0020d1d4775ffef93

Vinaykrishnan
  • 702
  • 3
  • 16
  • 30
  • But use of this throws an warning message : Block Based KVO Violation - Prefer the new block based KVO API with keypaths when using Swift 3.2 or later. – Jayprakash Dubey Apr 10 '18 at 06:56
  • There is a change in the syntax with swift 3.2, please check this example https://github.com/aguilarpgc/KVO – Vinaykrishnan Apr 10 '18 at 07:08