0

I'm loading a Web-View in my iOS App.

Is it possible to show an image while the Webview is loading in the background?

I found something for Objective-C but nothing for Swift 2.2.

How can I detect if the Web-View finished loading?

Edit:

   func webViewDidStartLoad(webView: UIWebView!) {
    loadingView.viewWithTag(1)?.hidden = false 
    print("Webview started Loading")
    }

func webViewDidFinishLoad(webView: UIWebView!) {
loadingView.viewWithTag(1)?.hidden = true 
print("Webview did finish load")
}

from: How to show image while page is loading in swift?

3
  • Provide some code, you have tried Commented Jul 10, 2016 at 15:31
  • edited my post. what I dont understand is where to place the image in the storyboard. I mean I cant place it over the webview?! Commented Jul 10, 2016 at 15:39
  • Check my edit @simplesystems Commented Jul 10, 2016 at 15:42

2 Answers 2

1

Conform to the UIWebViewDelegate protocol and set your view controller as the web view's delegate. The function webViewDidFinishLoad(_ webView: UIWebView) will be called when it has finished loading. You can set your image over the web view in the beginning then clear it in this function.

Edit:

I can't place it over the web view can I?

Yes, do exactly that. If you set the image to hidden it won't affect the user's interaction with the web view.

Sign up to request clarification or add additional context in comments.

5 Comments

@simplesystems What do you mean, the web view is replaced? If you place an image over the web view and then set it to hidden you should be able to see the web view, not the image. Does this not happen?
if you place an imagview above a webview in the storyboard.. then the webview is deleted.
@simplesystems ??? Drag a web view onto the storyboard. Drag an image over it. Now move the image and you'll see the web view hasn't been deleted. If it's not showing up when you run the program it's probably because you didn't set constraints.
... look at my solution above
Yes, that is a way to do it. But you could also add the image in the storyboard file @simplesystems
1

So finally its working...

Here my Solution for Swift 2.2

class YourViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {

    super.viewDidLoad()

    // For the Loading image: Create an ImageView programmatically and add id to the view
        let imageName = "YourPicture.png"
        let image = UIImage(named: imageName)
        let imageView = UIImageView(image: image!)
        imageView.tag = 1;
        imageView.frame = CGRect(x: 50, y: 250, width: 300, height: 100)
        view.addSubview(imageView)


      let url = "http://apple.com"

        let requestURL = NSURL(string:url)
        let request = NSURLRequest(URL: requestURL!)
        webView.delegate = self
        webView.loadRequest(request)        

    }

    func webViewDidFinishLoad(webView: UIWebView) {

       // UIWebView object has fully loaded.
      if(webView.stringByEvaluatingJavaScriptFromString("document.readyState") == "complete") {

        // Manually add a delay, showing the Webview takes some time..
        // For Swift 2.2
        let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 2 * Int64(NSEC_PER_SEC))

        dispatch_after(time, dispatch_get_main_queue()) {
        //put your code which should be executed with a delay here

           self.view.viewWithTag(1)?.hidden = true}
        }

        print("Webview did finish load")
    }

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.