0

I have a UIWebView set as the view in a ViewController. I would like to display an SVProgressHUD activity indicator while the webpage loads and then dismiss it when it has finished, but I can't seem to get it to work. The web page loads fine, but the indicator is never shown. I know SVProgressHUD is set up correctly and functions, but not in the functions I have.

I believe because I do not have a UIWebView actually in the scene in the IB, but rather the ViewController sets the view to a UIWebView, the code I am using cannot target it. Please correct me if I'm wrong.

This is my code:

import UIKit
import SVProgressHUD

class ViewController: UIViewController, UIWebViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()



        if let url = URL(string: "https://www.google.com") {
            let webView = UIWebView(frame: self.view.frame)
            let urlRequest = URLRequest(url: url)
            webView.loadRequest(urlRequest as URLRequest)
            webView.scalesPageToFit = true;
            webView.scrollView.showsHorizontalScrollIndicator = false;
            webView.scrollView.showsVerticalScrollIndicator = false;
            self.view.addSubview(webView)


        }
    }

    func webViewDidStartLoad(_ webView: UIWebView) {
        SVProgressHUD.show()
    }
    func webViewDidFinishLoad(_ webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
        SVProgressHUD.dismiss()
    }
}

Any help is greatly appreciated.

Aaron
  • 358
  • 2
  • 16

2 Answers2

5

UIWebView is deprecated, use WKWebView

import UIKit
import WebKit
import SVProgressHUD

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        if let url = URL(string: "https://www.google.com") {
            let webView = WKWebView(frame: self.view.frame)
            webView.navigationDelegate = self
            self.view.addSubview(webView)
            webView.load(URLRequest(url: url))
            SVProgressHUD.show()
        }
    }
}

extension ViewController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        print(error.localizedDescription)
        SVProgressHUD.dismiss()
    }

    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        print("Strat to load")
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("finish to load")
        SVProgressHUD.dismiss()
    }
}
Aaron
  • 358
  • 2
  • 16
PinkeshGjr
  • 8,460
  • 5
  • 41
  • 56
2

You forgot to bind delegates, so add this below line

webview.delegate = self

Note

UIWebView is deprecated, so use WKWebView

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
  • If I was to change to a WKWebView, how would I achieve the same thing I'm trying to achieve here. – Aaron Dec 19 '18 at 05:32
  • Instead I write code again, [here](https://iosdevcenters.blogspot.com/2016/05/creating-simple-browser-with-wkwebview.html) is the clean example please refer this/ Second, I tried sample here and there is no reason of getting error when you binds the delegate. it also working – dahiya_boy Dec 19 '18 at 05:35
  • Thank you so much. I'll move over to using WKWebView. I deleted my comment about the error as I realised I placed it into the wrong place. – Aaron Dec 19 '18 at 05:37