How to display the loading indicator on Swift 4 with WKViewWeb

0

Hello, I'm trying to show the loading indicator while my page is being read, but it did not work. I searched the internet, but I do not have the complete code and I'm a beginner in Swift. Can you 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.br")!
    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()
}

}
    
asked by anonymous 02.04.2018 / 00:40

2 answers

1

In your case, it does not go into the methods because you did not tell your WKWebView to associate its delegate with those implementing such a class. To solve this, just do this:

webview.delegate = self

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

    
04.04.2018 / 17:48
0

Dear colleague,

There are 2 questions you have to note when starting loading and stopping it:

1 - Does it start as hidden ?, if so you need to add:

indicador1.startAnimating()
indicador1.isHidden = false

2 - If this has already been done the second point is that all this part of screen iteration 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
}
    
09.05.2018 / 20:56