9

I have a tableView that utilizes the UIActivityViewController so users can share content on other 3rd party networks Facebook, twitter, text message etc. I have a function called IncreaseShareByOne() and it's purpose is to count the number of times that something has been shared . My problem is that right now that function fires of every time the UIActivityViewController is clicked . Is there someway that I can find out if a user really shared something so that I can use that function correctly ? becomes sometimes you open the UIActivityViewController and decide not to share anything so I want to catch and not use that function. I am new to swift and am using version 3 .

func sharedShareAction(controller: UIViewController, sender: UIButton) {

    controller.present(activityViewController,animated: true,completion: nil)
    IncreaseShareByOne()
}
toddg
  • 2,863
  • 2
  • 18
  • 33
Rome Torres
  • 1,061
  • 2
  • 13
  • 27

1 Answers1

15

You can add a completion handler to your UIActivityViewController. In the completion handler, check whether the user submitted using the completed value. You'll probably want to do something like this:

func sharedShareAction(controller: UIViewController, sender: UIButton) {

    controller.present(activityViewController,animated: true, completion: nil)

    activityViewController.completionWithItemsHandler = { activity, completed, items, error in
            if !completed {
                // handle task not completed
                return
            }
            IncreaseShareByOne()
        }
}

Check the API docs for more info.

toddg
  • 2,863
  • 2
  • 18
  • 33