0

I have a UIActivityViewControllerthat allows images to be shared to Facebook (if the system-wide Facebook account is configured).

This works as expected, but once the Facebook icon is tapped, there is a delay of several seconds before the Facebook sharing dialog is presented. The image is already in memory, so I am not certain what the lag is from, though I suspect it must be from contacting Facebook and waiting for a response.

I don't see any way possible to display an indeterminate UIActivityIndicatorView from the time the Facebook icon is tapped until the dialog is presented. Does anyone know of a way?

Jeshua Lacock
  • 5,730
  • 1
  • 28
  • 58

1 Answers1

0

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()

})
Paul Degnan
  • 1,972
  • 1
  • 12
  • 28
  • I am not using a button handler - the button is handled by Apple's SDK. I don't receive any events when the Facebook button is tapped from the UIActivityViewController; thats the problem. – Jeshua Lacock Mar 30 '17 at 22:51
  • Ah got it, it's really about the delay in the Facebook share screen coming up. Although they don't directly address this case, some of the answers in http://stackoverflow.com/questions/13519904/how-to-make-the-presentviewcontroller-with-slcomposeviewcontroller-faster may be helpful.. one describes how to preload `SLComposeViewController ` to make it faster, and the other suggests loading it async which prevents the app from slowing down – Paul Degnan Mar 31 '17 at 12:13