How to use an imgLoad before loading Webview

Asked

Viewed 290 times

1

In swift I can load a webView by doing the following:

1 - Add Uiwebview to my View.

2 - I create an Outlet called webView in my viewcontroller.Swift

3 - Insert the following code into my function viewDidLoad

 let url = NSURL (string: "http://answall.com");
 let requestObj = NSURLRequest(URL: url!);
 myWebView.loadRequest(requestObj);

It turns out that in this loadRequest I’m already calling my webView and it works, but I don’t know how to add an image by clicking to not leave that screen blank.

1 answer

2


Implement the delegate of Uiwebview to receive its status changes. When you start loading the webview you put something on the screen (image, Activity Indicator, HUD). When it finishes loading, either successfully or error, you remove it.

For example, to display an image while webview loads:

class ViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet weak var myWebView: UIWebView!
    var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = NSURL (string: "https://answall.com")
        let requestObj = NSURLRequest(URL: url!)
        myWebView.loadRequest(requestObj)
        myWebView.delegate = self
        addLoaderImage()
    }

    func webViewDidFinishLoad(webView: UIWebView) {
        removeLoaderImage()
    }

    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        removeLoaderImage()
    }

    func addLoaderImage() {
        imageView = UIImageView(frame: myWebView.frame)
        imageView.image = UIImage(named: "loadingWebView.png")
        self.view.addSubview(imageView)
    }

    func removeLoaderImage() {
        imageView.removeFromSuperview()
        imageView = nil
    }
}
  • All right, I added a hub called Mbprogresshub in cocados put the Uiwebviewdelegate and the two methods of it, I just found a bad thing, it really waits all page load to exit the effect of the load, I’m loading a wordpress site that is very slow :(

Browser other questions tagged

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