Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS swift how to know when activityViewController has been successfully used

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()
}
like image 655
Rome Torres Avatar asked Jul 15 '17 20:07

Rome Torres


1 Answers

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.

like image 126
toddg Avatar answered Oct 21 '22 08:10

toddg