Just had a similar problem! I wanted to execute an HTTP call to my web application, which was necessary so I would have the URL for a tweet. (In my case it was complicated further because the call to web application was on an async thread as well.) There are two ways to do it... you can add an Activity Indicator View to the storyboard and create IBActions as described here: http://sourcefreeze.com/uiactivityindicatorview-example-using-swift-in-ios/
I found it more straightforward to just add the UIActivityIndicatorView
via code. As a member of your class:
let activityIndicator = UIActivityIndicatorView( activityIndicatorStyle: UIActivityIndicatorViewStyle.gray )
I added the startAnimating()
and stopAnimating()
calls within new methods:
func startActtivityIndicator(){
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
self.view.addSubview( activityIndicator )
activityIndicator.startAnimating()
}
func stopActtivityIndicator(){
activityIndicator.stopAnimating()
activityIndicator.removeFromSuperview()
}
Then call startActivityIndicator()
as your first action in your button handler, and stopActtivityIndicator()
right before you show the facebook share. In my case, since I wanted to end the indicator and show the tweet box (using SLComposeViewController
in response to an async http request, I had to call back into the main thread to the stop method this way:
DispatchQueue.main.async(execute: { () -> Void in
self.stopActtivityIndicator()
})