Search code examples
iosswiftxcodeuiwebviewuiwebviewdelegate

Xcode 9 : Navigation item title of UIWebView is loaded after WebView is loaded


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?


Solution

  • 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