After full content load for webview then only hide loading indicator iOS swift

 Problem:

I want to show loading indicator before load of webview content till webview content fully loaded.

After content loaded hide the loader and show the webview.How to achieve that?

Solution:

    override func viewWillAppear(_ animated: Bool) {

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

        

        DispatchQueue.main.async {

            self.loadwebUrl()

        }

    }


    override func viewWillDisappear(_ animated: Bool) {

        super.viewWillDisappear(animated)

        webView.removeObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress))

    }


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

        if keyPath == "estimatedProgress" {

            //showLoading

        }

    }


    func loadwebUrl() {


}



extension ShopVC: WKNavigationDelegate, WKUIDelegate {

    

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

        stopLoading()

    }

    

    func webView( _ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {

        stopLoading()

    }

    

    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {

        showLoading()

    }

    func stopLoading() {

        webView.isHidden = false

        hideLoading()

    }

}

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift