I'm trying to display an Activity Indicator View in my app to users while a url is being loaded into a WebView. I've tried toying with activity.startAnimating/activity.stopAnimating and tried placing them in functions, etc. but have not had any luck.
The best I have been able to get is the Activity Indicator to appear and animate, but then not stop animating or hide once my url is loaded, so it continues spinning on top of the web page.
In other situations, when trying to move around activity.startAnimating, I have encountered the "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" issue. I have "Hides when Stopped" checked in the attributes inspector, I know that my url is valid, and I have created IBOutlets for the Interface Builder Elements.
Bear with me; I'm relatively new to Swift. Here's my code:
class HighchartsController: UIViewController {
@IBOutlet weak var HighchartsView: UIWebView!
@IBOutlet weak var activity: UIActivityIndicatorView!
@IBOutlet weak var saveButton: UIBarButtonItem!
@IBOutlet weak var highchartsMenu: UIBarButtonItem!
override func viewDidLoad()
{
super.viewDidLoad()
if self.revealViewController() != nil {
highchartsMenu.target = self.revealViewController()
highchartsMenu.action = "revealToggle:"
loadAddress()
}
}
func loadAddress() {
let url = NSURL (string: "http://google.com/flights")
let request = NSURLRequest (URL: url!)
HighchartsView.loadRequest(request)
println("Webpage Loaded Successfully")
}
}
And I have tried using different functions such as
webViewDidStartLoad(_ :UIWebView){
activity.startAnimating()
NSLog("Webview load has started")
}
webViewDidFinishLoad(_ :UIWebView){
activity.stopAnimating()
NSLog("Webview load had finished")
}
If you are using WKWebView, set WKNavigationDelegate like
webView.navigationDelegate = self
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
//Show loader
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//Hide loader
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
//Hide loader
}
extension WebViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
print("Start loading")
LoadingIndicator.show()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("End loading")
LoadingIndicator.hide()
}
}
Call the delegate methods. It's working fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With