I am trying to show MBProgressHUD
on UIWebView
. I am trying to load an iframe in webview. My spinner shows when webview starts loading, but doesn't hide even though it goes to the breakpoint in the webView didFinishLoad()
.
Here is my code :
class LiveViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var liveWebView: UIWebView!
var spinnerActivity: MBProgressHUD! = nil
override func viewDidLoad() {
super.viewDidLoad()
liveWebView.delegate = self
let embededHTML = "<html><body><iframe src=\"\" width=\"100%\" height=\"500\" frameborder=\"0\" scrolling=\"no\"></iframe></body></html>" //link removed
liveWebView.loadHTMLString(embededHTML, baseURL: nil)
}
public func webViewDidStartLoad(_ webView: UIWebView){
spinnerActivity = MBProgressHUD.showAdded(to: self.view, animated: true)
spinnerActivity.label.text = "Loading"
spinnerActivity.detailsLabel.text = "Please Wait!"
spinnerActivity.isUserInteractionEnabled = false
}
public func webViewDidFinishLoad(_ webView: UIWebView){
spinnerActivity.hide(animated: true)
}
public func webView(_ webView: UIWebView, didFailLoadWithError error: Error){
spinnerActivity.hide(animated: true)
}
}
I noticed that webViewDidStartLoad and webViewDidFinishLoad is being called twice. Please help me. The spinner is just not hiding.. I have used Xcode8 and Swift 3.
Edit :
I tried this but it doesn't work too :
class LiveViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var liveWebView: UIWebView!
var spinnerActivity: MBProgressHUD! = nil
override func viewDidLoad() {
super.viewDidLoad()
liveWebView.delegate = self
let embededHTML = "<html><body><iframe src=\"\" width=\"100%\" height=\"500\" frameborder=\"0\" scrolling=\"no\"></iframe></body></html>" //link removed
liveWebView.loadHTMLString(embededHTML, baseURL: nil)
}
public func webViewDidStartLoad(_ webView: UIWebView){
MBProgressHUD.showAdded(to: self.view, animated: true)
}
public func webViewDidFinishLoad(_ webView: UIWebView){
MBProgressHUD.hide(for: self.view, animated: true)
}
public func webView(_ webView: UIWebView, didFailLoadWithError error: Error){
MBProgressHUD.hide(for: self.view, animated: true)
}
}
See
Try to replace your code by this :
class LiveViewController: UIViewController, UIWebViewDelegate {
var isHudShow = false
@IBOutlet weak var liveWebView: UIWebView!
var spinnerActivity: MBProgressHUD! = nil
override func viewDidLoad() {
super.viewDidLoad()
liveWebView.delegate = self
let embededHTML = "<html><body><iframe src=\"\" width=\"100%\" height=\"500\" frameborder=\"0\" scrolling=\"no\"></iframe></body></html>" //link removed
liveWebView.loadHTMLString(embededHTML, baseURL: nil)
}
public func webViewDidStartLoad(_ webView: UIWebView){
if isHudShow == false
{
MBProgressHUD.showAdded(to: self.view, animated: true)
isHudShow =true
}
}
public func webViewDidFinishLoad(_ webView: UIWebView){
MBProgressHUD.hide(for: self.view, animated: true)
}
public func webView(_ webView: UIWebView, didFailLoadWithError error: Error){
MBProgressHUD.hide(for: self.view, animated: true)
}
}