Orkhan Alizade
Orkhan Alizade

Reputation: 7559

Progress bar while webView loading

I want to make that when tableView row tapped and opens the webView screen my progressBar appears with the value 0.3. Later it progress +0.1 and when it loaded it becomes 1 and hide it.

What I do:

progressBar.progress = 0.3
myWebView.loadHTMLString(body, baseURL: nil)
progressBar.progress = 1

It loads immediately full progress on tap. How can I do it? Must I turn off async?

UPDATE

I did also

func webViewDidStartLoad(webView: UIWebView) {
    progressBar.progress = 0.2
}

func webViewDidFinishLoad(webView: UIWebView) {
    progressBar.progress = 1
}

but it doesn't work. It stays in some position

UPDATE 2

They did not call yet. I wrote println inside of these functions and nothing printed

Upvotes: 0

Views: 5030

Answers (3)

Matej Marić
Matej Marić

Reputation: 125

Try this out

override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSURL(string: "https://topdigital.agency")
    let request = NSURLRequest(url: url as! URL)
    webView.loadRequest(request as URLRequest)
    webView.delegate=self

}

func webViewDidStartLoad(_ webView: UIWebView) {

    self.progressView.setProgress(0.1, animated: false)
}


func webViewDidFinishLoad(_ webView: UIWebView) {

    self.progressView.setProgress(1.0, animated: true)
}

func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {

    self.progressView.setProgress(1.0, animated: true)
}

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

Upvotes: 0

Orkhan Alizade
Orkhan Alizade

Reputation: 7559

I just drag from my webView and delegate it with my viewController and it works now!

Upvotes: 0

Sohel L.
Sohel L.

Reputation: 9540

You better see this tutorial: http://www.ioscreator.com/tutorials/progress-view-tutorial-in-ios8-with-swift

Well, your mistake in the code is:

When 'webViewDidStartLoad' will be called then progress bar will increase to 0.2%, that's fine! Because its the initial point. But when you are going to increase to 0.2 - 0.4 or 0.2 - 0.3, for that you have to run as an Async task. So, please look at the tutorial and increase the counter accordingly.

Hope you got it!

Upvotes: 1

Related Questions