0

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?

HATIM
  • 1

1 Answers1

0
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.
Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95
Patel
  • 43
  • 6
  • thank you for your answer, could you check these issue when I tried to applied it to my code: https://i.ibb.co/8BrsDpY/SO-1443-03-02-at-4-36-16-PM.png – HATIM Oct 08 '21 at 13:37
  • There is something problem with your progressHUD pods. One more thing, remove duplication of methods. If you have still doughts, you can give me the code link from GITHUB or WeTransfer. – Patel Oct 11 '21 at 11:54