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.
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()
}
}