How to show SVProgressHUD while loading a link inside iOS WebView

243 Views Asked by At

I am trying to show SVProgressHUD while loading a link inside the WebView, in others words, I have a WebView which has a link and I want show the SVProgressHUD when I hit that link.

here is my webiew code:

class ViewController: UIViewController, WKNavigationDelegate {
    @IBOutlet weak var webView:WKWebView?

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setNeedsStatusBarAppearanceUpdate()
    }
    override var preferredStatusBarStyle: UIStatusBarStyle {
        .lightContent
    }

    override func viewDidLoad()
    {

        super.viewDidLoad()

   
        webView?.navigationDelegate = self
        //webView?.addSubview(Spinner)
        SVProgressHUD.show(withStatus: ("Loading ..."))
        SVProgressHUD.setBackgroundColor(#colorLiteral(red: 0.3466703892, green: 0.7427461743, blue: 0.6237300038, alpha: 1))
        SVProgressHUD.setForegroundColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1))
        SVProgressHUD.setMinimumSize(CGSize(width: 120, height: 120))
        SVProgressHUD.setRingThickness(7.0)
        SVProgressHUD.setRingRadius(CGFloat(40.0))

        let request = URLRequest(url: URL(string: "https://luhom-app.com/")!)
        webView?.load(request)

        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: #selector(reloadWebView(_:)), for: .valueChanged)
        refreshControl.backgroundColor = #colorLiteral(red: 0.3466703892, green: 0.7427461743, blue: 0.6237300038, alpha: 1)
        webView?.scrollView.addSubview(refreshControl)
    }
    

    @objc func reloadWebView(_ sender: UIRefreshControl) {
        webView?.reload()
        SVProgressHUD.show(withStatus: ("Refreshing ..."))
        sender.endRefreshing()
        
    }
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        DispatchQueue.main.async {
    
        SVProgressHUD.showSuccess(withStatus: ("Done!"))
        SVProgressHUD.dismiss()
        }
        
    }

    
}

Any help on that please?

1

There are 1 best solutions below

2
Patel On
private func loadWebView(with url: String){
    
    guard let URL = URL(string: url) else {return}
    
    var request = URLRequest(url: URL)
    request.httpMethod = "GET"
    request.timeoutInterval = 10.0
    request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    
    webView.navigationDelegate = self
    webView.scrollView.showsVerticalScrollIndicator = false
    webView.scrollView.showsHorizontalScrollIndicator = false
    showProgressHUD()
    webView.load(request)
    
}

extension WebViewVC: WKNavigationDelegate{
    
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        // Hide LoadingView
        // Show ErrorView
    }
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        // Hide LoadingView
    }
    
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        // Hide LoadingView
        // Show ErrorView
    }
}
  • Your viewController needs to conform to the webViews navigation protocol/delegate.