David David - 4 months ago 35
Swift Question

Xcode: WebView refreshes/reloads after endRefreshing()

I have an iOS app build with Xcode and Swift.

When I pull to refresh the activityIndicator spins.
When I release the animation/spin will stop immediately but the website only refreshes/reloads after ca. 3/4-1 seconds.

Is there a way to show the animation/spin wheel till the end of refreshing?
This is my code:

func pullToRefresh() {
self.homewebview.reload()
self.refreshControl.endRefreshing()
// NSURLCache.sharedURLCache().removeAllCachedResponses()
}
func addRefreshControl() {
refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Einen Moment bitte…")
refreshControl.addTarget(self, action: #selector(FirstViewController.pullToRefresh), forControlEvents:.ValueChanged)
self.homewebview.scrollView.addSubview(refreshControl)
refreshControl.tintColor = UIColor.blackColor()
}

Answer

You need to call the endRefreshing method of UIRefreshControl in delegate method of UIWebViewDelegate like this

override func viewDidLoad() {
    super.viewDidLoad()
    self.webView.delegate = self
}

func webViewDidFinishLoad(webView: UIWebView) {
    self.refreshControl.endRefreshing()
}

Also remove self.refreshControl.endRefreshing() from pullToRefresh method.

Hope this will help you.

Comments