How to show loading indicator on Swift 4 with Wkviewweb

Asked

Viewed 101 times

0

Hi, I’m trying to show the loading indicator while my page is being read, but it didn’t work. I searched the internet, but it doesn’t have the full code and I’m a beginner on Swift. You can help?

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

@IBOutlet weak var indicador1: UIActivityIndicatorView!
@IBOutlet weak var webview: WKWebView!
override func viewDidLoad() {
    super.viewDidLoad()
    loadadress()
}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func loadadress(){

    let url: URL = URL(string: "http://www.google.com")!
    let urlRequest: URLRequest = URLRequest(url: url)
    webview.scrollView.isScrollEnabled = false;
    webview.scrollView.bounces = false;
    webview.load(urlRequest)
}

func webViewDidStartLoad(_: UIWebView){
    indicador1.startAnimating()
}
func webViewdDidFinishLoad(_:UIWebView){

    indicador1.stopAnimating()
}

}
  • How’s the UIActivityIndicatorView in the view? It appears but does not animate or it does not appear? Could you also check, please, if during the execution, the methods webViewDidStartLoad and webViewdDidFinishLoad execute?

  • It’s in View above webview, debug doesn’t go through didstart or didfinish, I put a print on these functions and didn’t generate anything in the console. When I activate the indicator manually I see over the webview and is activated.

  • Okay, so try to implement the didFinish of WKNavigationDelegate and call the indicador1.stopAnimating() in it. There the indicador1.startAnimating() you can call right after the webview.load(urlRequest) in his role loadadress().

2 answers

1

In your case, it does not enter the methods because you did not tell your Wkwebview to associate the delegate of it with those implementing such in that class. To solve this, just to do this:

webview.delegate = self

From there, your webview will call the methods you are implementing in this class when the webview delegates are triggered.

0


Dear colleague,

There are 2 questions you have to look at when starting the loading and stopping it:

1 - It starts as Hidden? if yes you need to add:

indicador1.startAnimating()
indicador1.isHidden = false

2 - If this has already been done the second point is that all this screen iteration part has to run on the main thread, try to add the following code:

No start loading:

DispatchQueue.main.async {
    indicador1.startAnimating()
    indicador1.isHidden = false      
}

No stop loading:

DispatchQueue.main.async {
    indicador1.stopAnimating()
    indicador1.isHidden = true
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.