How to update a UIWebView to include and remove a UIActivityIndicatorView

1

I'm having difficulty including and finalizing the display of a UIActivityIndicatorView ("loading animation"). I've already tried using webViewDidStartLoad and webViewDidFinishLoad, respectively to start and end the animation, but I was not successful. The code I'm using is this:

import UIKit

class ViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet weak var visaoWeb: UIWebView!
    @IBOutlet weak var carregamento: UIActivityIndicatorView!

    let urlString : String = "http://www.google.com.br"

    override func viewDidLoad() {

        if let url = NSURL(string: self.urlString) {
            let requestObj = NSURLRequest(url: url as URL)
            self.visaoWeb.loadRequest(requestObj as URLRequest)
        }

    }

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

    /*
    func webViewDidStartLoad(_ webView: UIWebView) {
        self.carregamento.startAnimating()
    }

    func webViewDidFinishLoad(webView: UIWebView) {
        self.carregamento.stopAnimating()
    }
     */

}

Note: I'm using swift 3.0

    
asked by anonymous 14.09.2016 / 19:58

1 answer

3

You're on the right path. There are some things that may be preventing your code from working properly.

  • For the methods of the delegate of UIWebView to be called it is necessary to pass to the webView which object will be its delegate, in the self case. You can do this by nib or in code (self.visaoWeb.delegate = self)
  • The code accesses a url with http protocol. By default App Transport Security will block this type of connection. Use https or add an exception to allow access by http . / li>
  • Check in the Storyboard if the connection to the outlets is correct (webview and upload)
  • Use the correct signature of the webViewDidFinishLoad method

    func webViewDidFinishLoad(_ webView: UIWebView) {
    
      self.carregamento.stopAnimating()
      self.carregamento.removeFromSuperview()
    }
    
  • 14.09.2016 / 21:22